Optimization of Salesforce Org - Everything You Need to Know
In this blog we’ll learn about Optimization, why is it necessary, how can we optimize apex class, the highest execution duration of an apex class, the highest execution duration of an SOQL and finally the highest execution duration of a Synchronous and Asynchronous transactions.
So, let’s get going.
It is the process of enhancing system capabilities through various methods. For example: Reducing the space/ time complexity of a code.
Why It Is Done?
- To increase the overall efficiency of the system.
- To improve the reliability, consistency, and accuracy of the system.
- To save time.
- For better resource utilization.
- To provide seamless experience to the user.
- To improve the overall performance of the system in order to meet business requirements.
Don't forget to check out: Queueable Apex vs Batch Apex | Salesforce Developer Guide
How Can we Optimize Apex Class?
Optimization can be done by following some points below:
- Don’t write DML and SOQL inside loops because if we run a code that will execute the query for over 100-150 times then governor limits would increase and we will get error of too many SOQL/ DML statements. To optimize the code, one should not use them within a loop.
- By running fewer queries, SOQL performance can be increased and system can be optimised.
- Ensure that the queries are selective.
- A query is said to be selective if one of the query filters is on an indexed field. But what does the query filter do?
- This filter reduces the resulting number of rows below a system-defined threshold. For example: When you apply a filter such as “select name from contacts where id=10”. Hence, the SOQL query performance improves.
- It can also improve further if you use two or more filters.
- Performance of an apex code can be increased by using the platform cache feature of the Salesforce Lightning platform. We can use this feature to reduce the server response time and use platform cache. But how can we enable this?
- By default, Developer org has cache capacity of 0 MB. We can request a trial cache of 10 MB.
- To request a trial, go to Setup in Developer org.
- In the Quick Find box, enter cache and then click Platform Cache.
- Click Request Trial Capacity and wait for the email notification of Platform Cache trial to be active. Salesforce approves trial requests immediately but it can take a few minutes to receive the mail.
5. Use of the limit apex methods can be done in order to avoid reaching the governor limits. Limits methods return the specific limit for a particular governor. For example: getLimitHeapSize() method returns the total amount of memory (in bytes) which can be used for the heap.
6. By using maps, we can avoid the nested for and do-while loops.
Check out another amazing blog by Tanya here: Multi-Factor Authentication in Salesforce - The Brief Guide
What must be the maximum execution duration of an apex class?
- Maximum execution time for each Apex transaction is 10 minutes.
What must be the maximum execution duration of an SOQL?
- Maximum SOQL query runtime before the transaction can be cancelled by Salesforce is 120 seconds.
Highest execution duration of a Synchronous and Asynchronous transactions?
- For synchronous transactions, Salesforce limits the CPU usage to 10 seconds and for an asynchronous transaction it must be 60 seconds.