Most of you should probably know that Salesforce is a multitenant environment that’s shared by hundreds of organizations and users worldwide. And just like in any environment, it’s resources aren’t infinite.
It’s no surprise that there often occur challenges when it comes to distributing and using these resources wisely. That said, since the resources are bounded, the system has established limitations, making sure that they aren’t overused and are shared fairly. This is exactly why Governor Limits are introduced in Salesforce.
In this article, we’ll show you the existing Governor Limits for Salesforce and give a couple of tips on how to bypass them.
- What are Governor Limits in Salesforce?
- Types of Governor Limits in Salesforce
- List of Governor Limits in Salesforce for Apex
- How to Bypass Governor Limits in Salesforce
What are Governor Limits in Salesforce?
Giving a definition of Governor Limits in Salesforce, they are sets of rules and regulations that restrict the monopolistic use of the resources on the cloud by this or that organization that’s using Salesforce services. Such limitations (aka governors) prevent the abuse of the resources (such as memory, database, number of processed records and script statements, etc) by establishing constraints that restrict the implementation of code and Apex runtime. Put simply, when explaining what is the use of Governor Limits in Salesforce, this also means that the executed code should be written optimally in terms of resource usage.
When a governor limit is surpassed or violated, Salesforce shows an error and pauses the process that caused the error.
Types of Governor Limits in Salesforce
The different Apex Governor Limits in Salesforce are the following
- Per-Transaction Apex Limits (such limitations are applicable for every Apex transaction; what’s for Batch Apex cases, limitations are reset);
- Per-Transaction Certified Managed Package Limits (such limits are applied to the packages installed to an org with a namespace, such packages were created by SFDC partners and have successfully passed the majority transaction limit security checks run by the AppExchange);
- Force.com Lightning Platform Apex Limits (such limitations are enforced by the platform as they aren’t particular to Apex transactions);
- Static Apex Limits (restricts loop and apex trigger sizes, callout sizes and times, trigger code unit numbers, etc);
- Size-Specific Apex Limits (as evident from the name, such limits are used to regulate sizes so that no inappropriately sized items are used in the classes).
There also exist other limits for non-Apex, some of these include:
- Inbound Email Limits,
- Push Notification Limits,
- Governor Limits for Salesforce API Requests,
- Chatter REST API Limits,
- SOAP API Call Limits,
- API Query Cursor Limits,
- Metadata Limits,
- SOQL and SOSL Governor Limits in Salesforce,
- Visualforce Limits.
Synchronous vs Asynchronous Execution
An Apex code can be run in two ways: synchronously or asynchronously. With the first, all of the code gets executed at once in a single go. In the second case, part of the process gets executed after some time in the background as a separate thread step. In such an async scenario, users don’t have the necessity to wait before the process gets finished, other benefits of asynchronous execution include bigger governor limits.
Note that based on these execution types, Salesforce limits can also vary.
List of Governor Limits in Salesforce for Apex
Major Governor Limits
Firstly, in this small Salesforce Governor Limits cheat sheet, let’s overview the important limits that everyone should know:
|Total number of SOSL queries issued in Salesforce||20|
|DML Governor Limits in Salesforce (Total number of issued statements per transaction)||150|
|Total number of records retrieved by a single SOSL query||2000|
|Total number of records which were retrieved by SOQL queries||50000|
|Total number of records retrieved by Database.getQueryLocator||10000|
|Salesforce Governor Limits for heap size total||6 MB/12 MB|
Per-Transaction Apex Limits
The limits in the table below are applied for every Apex transaction. In some cases, when the limits vary depending on whether the transaction is synchronous or asynchronous, they are indicated in separate columns.
|Overview||Governor Limits for Synchronous Transactions||Governor Limits for Asynchronous Transactions|
|SOQL queries issued (total number)||100||200|
|Records retrieved by SOQL queries (total number)||50000|
|Records retrieved by Database.getQueryLocator (total number)||10000|
|SOSL queries issued (total number)||20|
|Records retrieved by one SOSL query (total number)||2000|
|DML statements issued (total number)||150|
|Records processed as a result of DML statements, Approval.process, or database.emptyRecycleBin (total number)||10000|
|Stack depth total for any Apex invocation that recursively fires triggers because of insert, update, or delete statements||16|
|Callouts (HTTP requests or web services calls) per transaction (total number)||100|
|Max. cumulative timeout for all callouts (HTTP requests or Web services calls) per transaction||120 sec.|
|Max. number of methods with the future annotation allowed per Apex invocation||50||0 in batch and future contexts; 1 in queueable context|
|Max. number of Apex jobs added to the queue with System.enqueueJob||50||1|
|Allowed sendEmail methods (total number)||10|
|Heap size total||6 MB||12 MB|
|Max. CPU time on the Salesforce servers||10 thousand milliseconds.||60 thousand milliseconds.|
|Max. execution time per Apex transaction||10 min.|
|Max. number of push notification method calls allowed per Apex transaction||10|
|Max. number of push notifications that can be sent in each push notification method call||2000|
Per-Transaction Certified Managed Package Limits
As stated earlier, such limits are applied to third-party-developed packages, installed from the AppExchange. Certified packages have their own cumulative cross-namespace limits applied, browse details on them in the table below.
|SOQL queries issued (total number)||1100|
|Records retrieved by Database.getQueryLocator (total number)||110000|
|SOSL queries issued (total number)||220|
|DML statements issued (total number)||1650|
|Governor Limits in Salesforce for the total number of callouts (HTTP requests or web services calls) per transaction||1100|
|SendEmail methods allowed (total number)||110|
Platform Apex Limits
The following governor limits in Salesforce list refers to the limits that are handled by the platform:
|Max. number of asynchronous Apex method executions (these include Batch Apex Governor Limits in Salesforce, future methods, Queueable Apex, and scheduled Apex) for every 24-hour period||250,000 or the number of user licenses in your org multiplied by 200, whichever is greater|
|Number of synchronous concurrent transactions for long-running transactions (which take more than 5 sec per org.)||10|
|Max. number of concurrently scheduled Apex classes||100 (note that for the Developer Edition, the limit is 5)|
|Max. number of “Holding” status batch Apex jobs in an Apex flex queue||100|
|Max. number of batch Apex jobs queued or active concurrently||5|
|Max. number of batch Apex job start method concurrent executions||1|
|Max. number of batch jobs that can be submitted in a running test||5|
|Max. number of test classes that can be queued within 24 hours (not applicable to Developer Edition)||The greater of 500 or 10 multiplied by the number of test classes in the org|
|Max. number of test classes that can be queued per 24-hour period (sandbox and Developer Edition orgs)||The greater of 500 or 20 multiplied by the number of test classes in the org|
|Max. number of query cursors open concurrently per user||50|
|Max. number of query cursors open concurrently per user for the Batch Apex start method||15|
|Max. number of query cursors open concurrently per user for the Batch Apex execute and finish methods||5|
Static Apex Limits
This table collects the extra limitations regarding Apex callouts, queries, records, and when there’s more than one transaction.
|Default timeout of callouts (HTTP requests or Web services calls) in a transaction||10 sec.|
|Max. size of callout request or response (HTTP request or Web services call)||6 MB for synchronous Apex | 12 MB for asynchronous Apex|
|Max. SOQL query run time prior to transaction cancellation by Salesforce||120 sec.|
|Max. number of class and trigger code units in Apex deployment||5000|
|Batch size of Apex trigger||200|
|Batch size for loop list||200|
|Max. record number returned for a Batch Apex query in Database.QueryLocator||50 000 000|
Size-Specific Apex Limits
The limits in the table that follows restrict the sizes indicated within Apex code, including the number of characters and method size.
|Max. number of characters for a class||1 000 000|
|Max. number of characters for a trigger||1 000 000|
|Max. amount of code used by all Apex code in an org||6 MB|
|Method size limit||65,535 bytecode instructions in compiled form|
How to Bypass Governor Limits in Salesforce
Imagine the following scenario: your code is highly efficient but you still come across the situations when you can’t perform the action that you need due to the limits. This is exactly when the question of how to avoid Governor Limits in Salesforce arises.
There exist several ways of getting around the limits, including via asynchronous processing like queueables, time-based workflow rules, future methods, and batches, as well as by publishing Platform events (the last is stated among the best solutions).
Mentioning other tips for overcoming the limits, if you’re planning to work with over 50k records, it is wise to make use of Batch Apex. Plus, it is considered good practice to bulkify code and avoid SOQL queries and the use of DML statements within a loop (collections or streamlining queries are better choices for this).
To conclude, it is vital to keep in mind the limits that Salesforce has when working on your code and managing your org. If you still have some remaining questions on how to handle Governor Limits in Salesforce, you’re more than welcome to turn to us for professional Salesforce consulting.