We all strive to write APEX in the best way possible and in this post I will cover few unique guidelines that will help to make your code perform better.
1. Initialization of List
If the ask is to write a SOQL Query to fetch the accounts and store them in a list, 8 out 10 developers will write like below.
PS : Remember that initialization cannot be ignored all the time, but only for few specific scenarios as mentioned above
2. Fetch fields without mentioning them in SOQL
If there is a requirement to fetch the Id and Name of an account, most of us will write the SOQL like below :
But what if I tell you that Id field is not required to be explicitly mentioned in the SOQL Query and will be queried automatically. Along with the ID, RecordTypeId is one field which is also available for you.
Also not just this, when there is a relation among objects and your SOQL is on the child object, no matter whichever field you query from the associated parent record, parent record Id will be automatically fetched.
So while writing your SOQL's, have in thoughts that there are few fields available even without querying.
3. Get value of Formula Field without any DML / SOQL
There could be multiple instances where we do the DML first so that the formula field gets populated and then query for it to use in our logic. To elaborate my point, let me take an example. Consider a scenario where there is an object by name "Measurement" and is having 3 fields Length (Number), breadth (Number) and Area (Formula). The area is a formula field which holds the value of length multiplied with breadth. So, in-order to get the value of area, we need to first insert a record and then write a SOQL like below.
But we can completely avoid writing SOQL and DML for getting the value of the formula field by leveraging a pre-defined method called "recalculateFormuals". In the below example I have no SOQL/DML but able to get the value of formula field for an un-inserted record on the fly.
The above approach will be very helpful while writing test classes.
Reference : https://developer.salesforce.com/docs/atlas.ja-jp.apexcode.meta/apexcode/apex_class_System_Formula.htm
4 Replace .size() with .isEmpty() wherever applicable
Prior to performing any DML or SOQL where a collection variable is being used, we follow the practice of checking the size of collection to avoid un-necessary SOQL / DML.
So now the time that is consumed by the method. Size () for a collection size of 10000 will be very higher (as it has to traverse through every item present) than the time consumed by the method. IsEmpty() (As the check is made only the very first record). So whenever you are using. Size () > 0, replace it will. IsEmpty () to avoid time
..Bazinga..
Interesting things and really helpful
ReplyDeleteThank you.!!
DeleteSuch a nice blog..keep it up
ReplyDeletePoint 3 is best of all. You have avpided the use of DML and SOQL that are more time, CPU and DB consumig. Superb inbulit method explained.
ReplyDeleteThank you!
DeleteHI Aditya ,
ReplyDeleteThanks for sharing the valuable information .Its really helped us fine tune the apex
Thanks much!!!
Delete