What is Governor Limit in Salesforce?
In Salesforce, it is the Governor Limits which controls how much data or how many records you can store in the shared databases. Because Salesforce is based on the concept of multi-tenant architecture. In simple words, Salesforce uses a single database to store the data of multiple clients/ customers.
To make sure no single client monopolizes the shared resources, Salesforce introduced the concept of Governor Limits in Salesforce which is strictly enforced by the Apex run-time engine.
Types of Governor Limits in Salesforce
There are different type of governor limit in Salesforce. Here are list of different type of governor limit in Salesforce.
- Per-Transaction Apex Limits : These limits count for each Apex transaction. For Batch Apex, these limits reset for each execution of a batch of records in the execute method.
- Force.com Platform Apex Limits : These limits aren’t specific to an Apex Transaction and are enforced by the Lightning platform.
- Static Apex Limits : Apex Limits that are applied across all transactions
- Size-Specific Apex Limits : Apex Limits related to the size of code.
- Miscellaneous Apex Limits
- Email Limits
- Push Notification Limits
Learn more about all of them on Execution Governors and Limits post.
Salesforce Governor Limits Cheat Sheet
Below are some important Salesforce.com governor limits that you should know when planning to enter the Salesforce field or working on some Salesforce project.
Description | Synchronous Governor Limit | Asynchronous Limit |
Total number of SOQL queries issued1 | 100 | 200 |
Total number of SOSL queries issued in Salesforce | 20 | |
Total number of DML statements issued | 150 | |
Total number of records retrieved by a single SOSL query | 2000 | |
Total number of records retrieved by SOQL queries | 50000 | |
Total number of records retrieved by Database.getQueryLocator | 10000 | |
Total heap size | 6 MB/12 MB | |
Maximum cumulative timeout for all callouts (HTTP requests or Web services calls) in a transaction | 120 seconds | |
Maximum number of Apex jobs added to the queue with System.enqueueJob | 50 | 1 |
Maximum CPU time on the Salesforce servers5 | 10,000 milliseconds | 60,000 milliseconds |
Advantages of Governor Limits in Salesforce
Why Salesforce introduce the Salesforce governor limit? It some some advantage. Lets learn them in details.
- Governor limits in Salesforce prevent other org from using and hence executing lengthy code, which can take up a lot of space in the memory and even the entire cloud CPU.
- Apex has completely different or unique coding limits.
- These governor limits help us stay in the right space of coding with Apex.
How to overcome governor limits in Salesforce
As much as it is important for Salesforce developers to know all the relevant Governor Limits, it is also important for them to know how to overcome from Governor Limits in Salesforce. There are certain Apex best practices you can follow in order to avoid these limits.
As a developer, we need to ensure that our code is scalable and does not exceed the governor limits. Let understand how we can overcome governor limit with some examples. The most well-known limits are those around SOQL and DML limits in a single transaction.
1. System limit exception too many soql queries 101
In Salesforce we can encounter the Salesforce Governor Limits system limit exception too many soql queries 101 very often. System limit exception too many soql queries 101 is a governor limit set by Salesforce. This is a hard limit, means you can’t increase it by contacting Salesforce support.
How to fix Too Many SOQL queries : 101 error
What this error means and how we can go about solving it? Here is one example of code that can introduce Too many SOQL query errors when you will try to insert more than 200 records in Salesforce.
trigger ContactTrigger on Contact (before insert) {
if (Trigger.isInsert) {
for(Contact c : Trigger.new) {
Account accList= [SELECT Id, Name FROM Account WHERE Id =: c.AccountId];
c.LastName = accList.Name;
}
}
}
Why we will get SOQL 101 error? Because we are using SOQL query inside the for loop. Let see how to resolve this System.LimitException: Too many SOQL queries: 101 error.
- Do not have DML statements or SOQL queries in our FOR loop.
- use collection to avoid SOQL inside the for loop
- Bulkify Apex Trigger and follow Trigger framework to avoid recursive issue in your code
- Potentially move some business logic into future.
- Since the database time is not calculated in CPU time it is always better to explore the usage of aggregate SOQL for your business use case.
2. DML 150 Limit
The 2nd most well known Salesforce governor limit concerns the number of DML operations in a single transaction. As per the docs, the limit is 150. Here one simple code which will give you “Too Many DML Statements : 151” error.
for (Integer i = 0; i < 150; i++){
Account accountObject = new Account();
accountObject.Name = 'Test ' + i;
insert accountObject ;
}
How to fix Too Many DML Statements : 151
You should not use DML statement inside for loops and you should leverage collections to store the data and then when you do a DML operation on a collection, it only counts as one DML!
List<Account> accountList = new List<Account>();
for (Integer i = 0; i < 150; i++){
Account accountObject = new Account();
accountObject.Name = ‘Test ‘ + i;
accountList.add(accountObject);
}
insert accountList;
3. Apex CPU Time Limit Exceeded
Salesforce CPU time limits usage to 10 seconds for synchronous transactions and 60 seconds for asynchronous processing. “Apex CPU time limit exceeded” error means your transaction is taking too long and can’t be completed.
How to resolve CPU Timeout Error in Salesforce
- Write one Trigger per object
- Avoid using Process Builder
- Use one flow per object and per event
- Avoid nested for loop
- Stop Recursion in Apex Code.
4. Overcome Salesforce Governor Limits Using Platform Events
Mixed DML’S Operations, Too Many SOQL Queries, Too Many DML Statements, CPU Timeout: Salesforce’s Governor limits are there for a reason but even when you employ best practices you may still exceed them. A good developer will look at all tools available on the platform and find the best approach to solving the problem they are facing.
Check out our old session how to overcome with Salesforce governor limit using platform events.
Summary
There are many different types of governor limits in Salesforce. There are some general patterns and Apex best practices can help you stay out of governor limits. The most well-known limits are those around SOQL and DML limits in a single transaction.