improve Apex Code Unit using Salesforce Best practices

Improve your Apex Code Unit | Salesforce Best Practices

In this blog, we are going to discuss the best practice of Apex. Apex code is used to write custom business logic.  in any language, we have to follow the syntax and best practices. which makes our code clean and efficient. Now we also have the best practice in Salesforce apex which we are going to discuss here.  

Let’s understand why we need Best Practice

Best practice recommended by Salesforce because apex operates in a densely populated area, the Apex operating time engine imposes strict limits to ensure that the apex code for runways does not use shared resources. 

Bulkify Apex Code 

Here the Bulkifying meaning is that we handle more than one record at a time. If we get more than one record, then at that time our code will not execute then we always consider the scenario of nullification. Here is an example of bulk action. 

for(Account account:trigger.New){
    // add logic here

dont miss out iconDon't forget to check out: Document Generation in Salesforce with Nintex DocGen via Apex Code

Make Reusability of Code

As we can understand through that line. We always need to make helper classes and define methods in them. If we need that method functionality then we can call that class and use that method. So that is how we make our code reusable. 

Avoid Nesting Loops within Loops

Nested loops should be avoided in Apex controllers as they may slow down page processing or may hit the governing limits for the page. Another easy way that gives a nice structure is to make the inner loop a different function or to reduce the use of loops completely. 

We usually encounter code when we have two or three or more barriers generated that affect performance. An easy way to avoid nesting traps is to use Maps. For example, you can find or create a key for each second loop item and enter a key and value on the map. 

Avoid DML & SOQL Inside ForLoops

As we know, the concept of governor limits that our DML operation has a specific limit. Such as limit of 150 you can perform 150-time DML operations. If the loop is running more than 150 times then it will break our code. 

Here is a best practice example. 

Map<Id,Account> accMap=new Map<Id,Account>({Select id,name,(select id from contacts)
from account where id in:trigger.newmap.keyset()});
for(Account acc:accMap.values()){
    For(Contact con:acc.Contacts){}

Use Database Methods while Doing DML Operation

Basically, the Database method and DML are the same operation-wise. But the point is that database method are more flexible compared with DML. 

  1. there is no term in DML operation. But there is a partial operation in the database methods.
  2. in DML we cannot get the list of successful and failed records. But we can get the same in the database methods.

Here Is an example of how should be used the database methods: 

Database.saveResult[] accuntResult=
work on failure records
for(Database.SaveResult sr:accountResults)
        //add ypur logic here
        for(Database.Error err:sr.detErrors()){}

dont miss out iconCheck out another amazing blog by Navdita here: Learn About Lightning-record-form Component in Salesforce

Querying Large Data Sets

In the salesforce ecosystem, a SOQL can return up to a record 50,000. If we are working on large data sets that result then we should always use SOQL query. 

Here is an example of how should be used SQL query for large data sets. 

for(List<Account> accountList:[select Id,name from Account where
BillingCountry Like '%India%']){
    //add your logic here


Popular Salesforce Blogs