Home SalesforceApex Optimizing Salesforce Apex Code

Optimizing Salesforce Apex Code

by Dhanik Lal Sahni

To handle business requirements, we have to do a lot of customization in Salesforce Application. Mostly we customize apex code on record DML operation such as when the record will be created or updated perform some business logic or on deleting record do some validation logic. This customization adds complexity to our application and if it is not coded well then it will impact our application’s performance. This post will help in Optimizing Salesforce Apex Code which is added for handling business requirements.

Below are all possible code optimization that can be done while querying it or doing DML operations in apex code.

  1. DML or SOQL Inside Loops
  2. Limiting data rows for lists
  3. Disabling Debug Mode for production
  4. Stop Describing every time and use Caching of the described object
  5. Avoid .size() and use .isEmpty()
  6. Use Filter in SOQL
  7. Limit depth relationship code
  8. Avoid Heap Size
  9. Optimize Trigger
  10. Bulkify Code
  11. Foreign Key Relationship in SOQL
  12. Defer Sharing Rules
  13. Use collections like Map
  14. Querying Large Data Sets
  15. Use Asynchronous methods
  16. Avoid Hardcode in code
  17. Use Platform Caching
  18. Use property initializer for test class
  19. Unwanted Code Execution

We will discuss all above approaches one by one in the code practice series. Let us see the first good code practice (DML or SOQL Inside Loop) in this post.

DML or SOQL Inside Loops:

SOQL is a query language that retrieves data from salesforce objects and DML is a data manipulation language that inserts/updates or deletes records in Salesforce Object.

What problem can occur If we use DML or SOQL in loops?

Salesforce has a government limit of 100 SOQL and 150 DML in one transaction. If we write code that will execute query over 100 or DML over 150, we will get Error: System.LimitException: Too many SOQL queries: 101 or  System.LimitException: Too many DML statements. So we should code in such a way that it should not reach that threshold.

So let us see examples where we can face limit exception and what are ways we can avoid DML/SOQL inside the loop.

Use Case:

Take an example, we have Account object which holds insurance customer information. When a record is updated, we will update the related address object. This is a very simple requirement and we can write the below code for this. Yes, we can do this using flow but for understanding, we will write code.

DML Inside Loop and SOQL Inside Loop

If we see the above code, it will perfectly work when you take action from UI like the lightning page, as it will take action for single record only. But assume later on, Account object will be updated from any data import tool or it will be updated in a batch job which will update thousands of records at once. In that case above code will work?

No, it will not work and throw the above-mentioned System.LimitException error. Why? If you see the above code carefully, we are using SOQL in a loop and we can only query 100 SOQL in one transaction so the above code will not work. So how to handle this SOQL limit exception.

DML inside loop

We can resolve SQOL limit exception but getting all accout ids in loop and use that account ids in SOQL once (like line# 3-10). Now above code will not throw exceptions as we have handled SOQL inside for loop issue. But when this trigger executes from the data update tool, it will still throw Too many DML statements exception when records are more than 150 to update. Let us handle that exception also

DML and SOQL inside loop resolved | Optimizing Salesforce Apex Code

Instead of updating record in loop we can update fiels in collection and update in object atonce (code line# 11-15). Now our code is good in respect to DML or SOQL inside a loop. Our SOQL is not in the loop and DML also is not in the loop. There is still one issue in code related to unwanted code execution which we will see in later posts.

Summary:

Apex code executed in an atomic transaction. When we code, we should avoid more than 100 SOQL or 150 DML statements in single transaction. While coding, we should never think about single record processing, always code which will work in bulk record processing. We should also consider that particuler code block can be excuted from other record insertion or updation. Like account trigger can be fired from contact record upation also, so no of SQOL can increase if we have multiple depedent objects. We will see these fixes in later posts.

This optimization technnque is also applicable for flow execution. If we have created a record trigger flow then that flow will execute for each record when the bulk record is processed. So a similar approach is required in flow also.

Similar Posts:

 Avoid Batch Apex and Use Queueable Class

References:

Error: System.LimitException: Too many SOQL queries: 101

You may also like

6 comments

Optimize Code by Disabling Debug Mode | SalesforceCodex January 5, 2022 - 7:42 pm

[…] Optimizing Salesforce Apex Code […]

Reply
Optimize Apex Code by Metadata Caching | SalesforceCodex January 20, 2022 - 11:24 pm

[…] Optimizing Salesforce Apex Code […]

Reply
Optimize SOQL Filter in Apex Code | SalesforceCodex February 2, 2022 - 4:14 pm

[…] Optimizing Salesforce Apex Code […]

Reply
Queueable Vs Batch Apex In Salesforce June 30, 2022 - 10:05 am

[…] Optimizing Salesforce Apex Code […]

Reply
Boost Performance of Salesforce Service Cloud - Vagmine Cloud Solution December 19, 2023 - 8:43 pm

[…] 11. Optimize Apex Code […]

Reply
Questions for Tech Lead/Salesforce Architect Interview March 24, 2024 - 10:03 pm

[…] Optimizing Salesforce Apex CodeDML or SOQL Inside LoopsLimiting data rows for listsStop Describing every time and use Caching of the described objectUse Filter in SOQLOptimize TriggerBulkify CodeForeign Key Relationship in SOQLHandle Heap Size for Apex Code Optimization […]

Reply

Leave a Comment