Tuesday, 27 August 2024

use of Type.forName() in apex

 How can we put Type.forName() to better use in Apex?


In Salesforce Apex, there's a method called `Schema.getGlobalDescribe()` that helps developers get information about all the objects in Salesforce. 


This can be useful, but it can also use up a lot of computer resources, like CPU and memory. 


This method is like a double-sided sword! 


You can take advantage of it however, if things go wrong it can hurt you too. 


Let's focus more on the downsides.


This can be a problem, especially if your Salesforce organization has many custom objects and fields, and it can cause your code to run slowly and hit some limits on how much CPU time it can use.


Imagine you want to get information about just one object.


But if you use this method incorrectly, you might end up getting information about all the objects in your Salesforce.


This can slow things down and cause issues. 


This is called an anti-pattern. (Now what's an anti-pattern? We will save it for another day).


What's the solution then?


You will take advantage of  'Type.forName'.


Find the code below to understand how to use it.




No comments:

Post a Comment