Tuesday, December 20, 2022

Governor Limits in Salesforce

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

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.

  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

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.

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

Advantages of Governor Limits in Salesforce

Why Salesforce introduce the Salesforce governor limit? It some some advantage. Lets learn them in details.

  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 completely different or unique coding limits.
  3. 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

  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.

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.

No comments:

Post a Comment