Governor Limits
Governor limits in Salesforce Apex are a set of runtime limits enforced by the Salesforce platform to ensure efficient use of shared resources across the multitenant environment. These limits are crucial for maintaining system performance and stability, preventing any single tenant (organization) from consuming excessive resources that could affect other tenants.
Here’s an overview of the key governor limits in Salesforce Apex:
1. SOQL Query Limits
- Total Number of SOQL Queries: In a single transaction, you can execute up to 100 SOQL queries (50 for synchronous transactions and 200 for asynchronous transactions). If you exceed this limit, the transaction fails with a runtime exception.
- Number of Records Returned: A single SOQL query can return a maximum of 50,000 records. If you need to process more than this, consider using pagination (OFFSET and LIMIT) or batch processing.
2. DML Operation Limits
- Total Number of DML Statements: In a single transaction, you can execute up to 150 DML statements. If you exceed this limit, the transaction fails.
- Total Number of Records Processed by DML: You can process a maximum of 10,000 records in DML operations per transaction.
3. CPU Time Limit
- CPU Time: Each transaction has a maximum of 10,000 milliseconds (10 seconds) of CPU time on the Salesforce servers. This includes time spent processing Apex code, executing queries, and running other operations. If your transaction exceeds this time, it will be terminated with a runtime exception.
4. Heap Size Limit
- Heap Size: The maximum amount of memory your Apex code can use at runtime is 6 MB for synchronous transactions and 12 MB for asynchronous transactions. The heap size includes all the objects, collections, and other data structures in memory. If your transaction exceeds this limit, it will fail with a runtime exception.
5. Total Number of Callouts
- Callouts: A single transaction can make up to 100 HTTP or web service callouts to external systems. This limit ensures that Salesforce remains responsive even when interacting with external systems.
6. Total Number of Records Retrieved by Database.getQueryLocator
- Query Locator: When using
Database.getQueryLocator
in a batch Apex context, you can retrieve up to 50 million records in a single query.
7. Total Number of SOSL Queries
- SOSL Query Limit: You can execute up to 20 SOSL queries in a single transaction. If you exceed this limit, the transaction fails with a runtime exception.
8. Execution Time for Transactions
- Maximum Execution Time: A single synchronous transaction has a maximum execution time of 5 minutes, while asynchronous transactions can run for up to 60 minutes. Transactions that exceed these times are terminated.
9. Total Number of Future Method Calls
- Future Method Call Limit: You can make up to 50 future method calls per transaction. Future methods are used to run asynchronous code and are often used to avoid hitting other limits in the synchronous execution context.
10. Email Limits
- Email Sent: The number of emails that can be sent per transaction is limited to 5,000 single emails. This includes both email messages sent through Apex and those triggered by workflows.
11. Total Number of Batch Apex Jobs
- Batch Jobs: Up to 5 active batch Apex jobs can be queued or running concurrently in an organization. There’s also a limit of 250,000 records processed per batch execution.
12. Total Number of Concurrent Long-Running Requests
- Long-Running Request Limit: You can have up to 10 concurrent long-running Apex requests that run longer than 5 seconds. Exceeding this limit results in additional requests being denied until the current ones complete.
Importance of Governor Limits
Governor limits are designed to:
- Ensure System Performance: By limiting resource usage, Salesforce ensures that the platform remains responsive and that no single tenant monopolizes system resources.
- Prevent Infinite Loops and Excessive Resource Consumption: Limits help developers avoid creating code that could run indefinitely or consume too many resources, which could bring down the entire system.
- Encourage Efficient Code: Developers are encouraged to write efficient, optimized code that operates within these constraints, leading to better-performing applications.
Best Practices to Avoid Hitting Governor Limits
- Bulkify Your Code: Always design your Apex code to handle bulk data operations efficiently. Avoid performing DML or SOQL operations inside loops.
- Use Collections: Leverage collections (lists, sets, maps) to process data in bulk rather than one record at a time.
- Optimize SOQL and SOSL Queries: Retrieve only the necessary fields and records. Use indexed fields in filters to speed up queries.
- Leverage Asynchronous Processing: When dealing with large datasets or long-running processes, consider using asynchronous Apex (batch Apex, Queueable Apex, future methods) to avoid synchronous transaction limits.
- Monitor and Analyze: Use Salesforce’s built-in tools like debug logs, the Developer Console, and Governor Limits warnings to monitor your transactions and adjust your code accordingly.
Understanding and adhering to these governor limits is crucial for developing scalable and efficient applications on the Salesforce platform.