Performance

Apex Governor Limits

Understanding Governor Limits

Apex governor limits restrict SOQL DML and CPU usage.

What are Apex Governor Limits?

Apex governor limits are runtime limits enforced by Salesforce to ensure efficient resource usage and code execution within the multi-tenant environment of the Salesforce platform. These limits are essential to maintain the performance and scalability of the system, preventing any single or poorly written code from monopolizing shared resources.

Types of Governor Limits

Apex governor limits can be broadly categorized into:

  • SOQL Query Limits: Restrict the number of SOQL queries that can be executed in a single transaction.
  • DML Statement Limits: Constrain the number of DML operations (Insert, Update, Delete, etc.) that can be performed in a transaction.
  • CPU Time Limits: Limit the CPU time that can be consumed by a transaction.
  • Heap Size Limits: Define the maximum amount of memory that can be used during code execution.

SOQL Query Limits

In Salesforce, the platform imposes a limit on the number of SOQL queries that can be executed in a single transaction. This limit is currently set to 100 for synchronous operations and 200 for asynchronous operations.

DML Statement Limits

The number of DML statements (such as insert, update, delete, and undelete) that can be executed in a single transaction is limited to 150. Exceeding this limit will result in a runtime exception.

CPU Time Limits

Salesforce imposes limits on the CPU time consumed by the code execution. The limit is 10,000 milliseconds (10 seconds) for synchronous transactions and 60,000 milliseconds (60 seconds) for asynchronous transactions. This ensures that no single transaction can monopolize server resources.

Strategies to Work Within Governor Limits

To work efficiently within the Apex governor limits, consider the following strategies:

  • Bulk Processing: Use collections such as lists and maps to handle multiple records in a single operation.
  • Efficient Querying: Optimize SOQL queries to retrieve only the necessary data.
  • Future Methods: Leverage asynchronous processing to offload tasks that are not time-sensitive.
  • Batch Apex: Use batch processing for handling large datasets.

Performance