Governor Limits in Salesforce

Today, we will talk about Governor limits in Salesforce and how to solve them. Governor Limits are a Salesforce developer’s biggest challenge. That is because if the Apex code ever exceeds the limit, then Salesforce throws the governor limit and issues a run-time exception that cannot be handled. Hence, as a Salesforce developer, you must be very careful while developing your application.

What is Governor Limit in Salesforce?

In Salesforce, it is the Governor Limits that control 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 ensure 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 types of governor limits in Salesforce. Here is a list of other type of governor limit in Salesforce.

  1. 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.
  2. Force.com Platform Apex Limits: These limits aren’t specific to an Apex Transaction and are enforced by the Lightning platform.
  3. Static Apex Limits: Apex Limits that are applied across all transactions
  4. Size-Specific Apex Limits: Apex Limits related to the size of code.
  5. Miscellaneous Apex Limits
  6. Email Limits
  7. Push Notification Limits
Types of Governor Limits in Salesforce
Types of Governor Limits in Salesforce

Learn more about all of them on Execution Governors and Limit post.

Salesforce Governor Limits Cheat Sheet

Here are some critical Salesforce.com governor limits that you should know when planning to enter the Salesforce field or working on some Salesforce project. Here are different types of governor limits.

Per-Transaction Apex Limits

DescriptionSynchronous
Governor Limit
Asynchronous Limit
Total number of SOQL queries issued1100200
Total number of SOSL queries issued in Salesforce20
Total number of DML statements issued150
Total number of records retrieved by a single SOSL query2000
Total number of records retrieved by SOQL queries50000
Total number of records retrieved by Database.getQueryLocator10000
Total heap size6 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.enqueueJob501
Maximum CPU time on the Salesforce servers510,000 milliseconds60,000 milliseconds

Static Apex Limits

DescriptionLimits
The default timeout of callouts (either HTTP requests or Web services calls) in a transaction10 seconds
The maximum size of the callout request or response (either HTTP request or Web services call)6 MB for synchronous Apex or 12 MB for asynchronous Apex
The maximum SOQL query runtime before Salesforce cancels the transaction120 seconds
The maximum number of class and trigger code units in the deployment of Apex7500
Apex trigger batch size200
For loop list batch size200
The maximum number of records that are returned for a Batch Apex query in Database.QueryLocator50 million

Advantages of Governor Limits in Salesforce

Why did Salesforce introduce the Salesforce governor limit? It has some advantages. Let’s learn them in detail.

  1. 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.
  2. Apex has wholly different or unique coding limits.
  3. These governor limits help us stay in the right coding space with Apex.

How to overcome governor limits in Salesforce

As much as Salesforce developers need to know all the relevant Governor Limits, it is also essential for them to understand how to overcome Governor Limits in Salesforce. There are certain Apex best practices you can follow to avoid these limits.

As developers, we must ensure that our code is scalable and does not exceed the governor’s limits. Let’s understand how we can overcome the 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 SQL queries 101

In Salesforce, we can encounter the Salesforce Governor Limits system limit exception to too many SQL queries 101 very often. System limit exception too many soql queries 101 is a governor limit set by Salesforce. This is a hard limit, so you can’t increase it by contacting Salesforce support.

How to fix Too Many SOQL queries: 101 error

What this error means, and how can we go about solving it? Here is one example of code that can introduce Too many SOQL query errors when inserting 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 will we get an SOQL 101 error? Because we are using SOQL query inside the for loop. Let’s 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 the collection to avoid SOQL inside the for-loop
  • Bulkify Apex Trigger and follow the Trigger framework to avoid the recursive issues in your code
  • Potentially move some business logic into the 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.

Learn more about How to Resolve the System.limitException: Too many SOQL Queries 101.

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 is one simple code that will give you the “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 a DML statement inside for loops and 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

  1. Write one Trigger per object
  2. Avoid using Process Builder
  3. Use one flow per object and per event
  4. Avoid nested for loop
  5. Stop Recursion in Apex Code.

Learn more about Apex CPU Time Limit Exceeded.

4. Too many Query rows

You will get LimitException: Too many query rows: 50001 error when your query returns more than 50,000 records. Solution: Use Limit and filter in your SOQL query.

Learn more about Different types of Exceptions in Salesforce.

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 on how to overcome with Salesforce governor limit using platform events.

Also, learn about how to log the Exception using Platform Events.

Summary

There are many different types of governor limits in Salesforce. 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.

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 458

One comment

  1. We can see some of Synchronous Governor Limit and Asynchronous Limit are same.
    when are saying Asynchronous Limit which apex function are involved.
    i am asking this question because Database.getQueryLocator returning 10000 records in synchronous and Asynchronous. but Batch apex ( which is Asynchronous) returning upto 50m records ?

Leave a Reply

Your email address will not be published. Required fields are marked *