Just like any SaaS based application, Salesforce Governor Limits restrict the excess usage of shared resources on the cloud, thereby enabling Salesforce developers to write efficient code. This article helps the readers to understand what they rules are. Therefore you can write efficient APEX codes and meet your goals by using application in an optimized manner.
Salesforce Governors Limits ensures that no one particular instance eat up entire resource and hence adversely impact other customers who are using the same server resources.
Related – See how to use Salesforce REST API for third party integrations.
What are the Salesforce Governors Limits
Following are the common Governor limits which every developer must be aware of. This will help solve unwanted issues and lags within the system. They are classified under various sections like APEX, Salesforce Web-service calls, SOQL and SOSL so that you can narrow down on your interest areas.
|Group||Description of Group||Limit Parameter|
(This section describes the parameter on which limit exists)
|SOQL||If you are aware of how we use SQL queries within applications, SOQL is exact equivalent of the same within Salesforce.||Maximum number of rows you can fetch in a SOQL|
Maximum number of queries you can initiate
|SOSL||If you are performing text searches across objects this translates to SOSL queries within Salesforce.||Maximum number of records you can fetch in a SOSL query|
Limit on number of SOSL query you can issue
|DML||Data manipulation language operation to perform CRUD operation in a mass manner||Total number of DML statements issues|
Maximum number of records which can be processed on DML operations
|HTTP requests||These are web service calls which can be initiated from other applications to Salesforce. This is crucial if you are looking at integration solutions within the application.||Total number of call-outs in a transaction|
Total cumulative timeout on web-service call in a transaction
|APEX||If you have custom logic developed, this would be done in APEX programming language. There are certain limits which apply to APEX developments||Maximum processing time for an APEX transaction|
Maximum push notification from APEX transaction
Now, we will be discussing about how to optimize the developments so that we never hit these limits. Some Governor’s limits are based on the transactions. A transaction is a set of actions that can be executed as a single unit. These could be class or triggers from Salesforce development perspective. For simplicity sake we will see how to work around the following limits
#1 Limit on DML operations per transaction
#2 Limit on SOQL per transaction
#3 Limit on number of records which can be processed within a DML operation
Going through these examples, you may be able to find solutions and best practices around other limits as well.
1. Maximum Number of DML Operations per transaction is 150
DML stands for ‘Data Manipulation Language’ which modifies the data using five operations. The 5 DML operations are insert, update, delete, merge and restore. Data in Salesforce can be managed using these 5 operations.
The number of DML operations allowed per a transaction is 150. So, the code should be written in such a way that this limit should not be reached. Below example shows the code with 150+ DML operations.
- In the above screenshot, insert is called thrice in line numbers 5, 9 and 13.
- In line no 5, insert will be executed once.
- In line number 9, insert is in for loop which is repeating for 150 times. So, insert operation will be executed 150 times.
- In line number 13, insert is executed once.
- Total number of times insert will be executed is 152
Now, execute the class by navigating to Debug ->’Open Execute Anonymous Window’
Enter the following code and click on ‘Execute’ button
DMLLimit dl=new DMLLimit(); dl.test();
Now, the following screen will be shown which says ‘Too Many DML statements’
Here we’re writing ‘DML statement’ (insert) inside the ‘for loop’. So, it’s executing 150 times.
This can be avoided by using ‘List’ in the above class. Similar operations of an object can be added to a list and perform DML operation on the list. In this way, the above code can be executed without hitting ‘Governors Limit’. Below is the modified code which doesn’t hit governor limits.
- In the above code, a list is created in line no 3 which holds ‘Account’ data.
- In line no 5, account ‘a’ is added to list ‘acclist’.
- In line no 9, account ‘acc’ is added to the same list and insert operation is not performed in the loop.
- In line no 13, account ‘a1’ is added to the list ‘acclist’.
- Insert operation is performed in line no 14 on the ‘acclist’. As all the accounts are added to ‘acclist’, all records can be inserted using the single insert operation on the list ‘acclist’.
In this way, we can avoid reaching ‘Governor limit’ while using DML statements in a transaction.
2. Maximum Number of SOQL Queries per transaction is 100
This is another important Salesforce Governors limit. Salesforce provides ‘Salesforce Object Query Language’ to access the records existing in Salesforce. ‘SOQL’ queries are executed in ‘APEX’ to retrieve data from Salesforce. Salesforce allows only a maximum of 100 SOQL queries per transaction. So, the code should be written in such a way that this limit should not be reached. Below example shows the code with 100+ SOQL statements.
In the above example, ‘SOQL’ query is written within for loop. So, ‘SOQL’ query executes for every ‘for loop’ iteration. An account can have 1 or more than 1 contact. If there are 100 accounts, the probability of the number of contacts is greater than or equal to 100. In this case, Governor limits will be reached and class fails to execute. Below screenshot shows the error for the same.
Following screenshot shows the correct code written without hitting Governors limit. Here, ‘SOQL’ query is written outside the ‘for loop’ which retrieves the contact details of Account and operations on performed on the list.
3.Maximum Number of records in a transaction on which DML operations can be performed is 10,000
The below example shows inserting records to ‘Account’ object. A list is created to store the records which need to be inserted.
- The First record is added to list in line no5.
- The Second record is added to list in 7.
- As the list is in ‘for loop’, 10,000 records will be added to list in line no 11.
- The total number of records to be processes 10002.
When above class is executed, Salesforce throws the error as shown below. This can be avoided by using ‘Batch Apex’.
These above listed are the most common Salesforce Governor limits faced by every developer. Apart from this, there are few other limitations imposed by Salesforce. All the Governor limits imposed by Salesforce can be seen from the developer console by following the steps given below.
Open developer console -> Navigate to Debug ->’Open Execute Anonymous Window’ -> Enter ‘system.debug(‘test’);’ and execute this.
Following Governor limit screen will be shown.
From the above scenarios we can find that Salesforce Governor Limits are reached by writing SOQL and DML statements in ‘for loops’. We can prevent this by following above principles.
How we can help
If you have any Salesforce development or integration needs we can help you achieve the same. Contact us for a free assessment.