![]() |
VOOZH | about |
Salesforce operates in a multitenant environment, where multiple organizations share the same system resources. To ensure fair resource allocation, Salesforce imposes governor limits that define the maximum resource usage for each organization or process. These limits are critical for maintaining system performance and stability, preventing any single operation or tenant from consuming excessive resources.
This article explores the concept of governor limits, why they exist, common limits, and best practices for working within them efficiently. Examples and solutions are provided to help you avoid and resolve governor limit issues.
Governor limits are restrictions set by Salesforce to ensure efficient resource use in a multitenant environment. These limits apply to various resources such as database operations, memory, and processing time.
In Salesforce’s multitenant architecture, a single server hosts multiple Salesforce organizations (orgs). This is comparable to an apartment building where residents share utilities like water, electricity, and internet. Without limits, one tenant’s excessive usage could negatively impact others. Governor limits act as safeguards, ensuring equitable resource distribution among all tenants.
Example:
Salesforce governor limits can be broadly categorized into the following types:
These limits apply to individual Apex transactions. For example:
These limits are not tied to a specific transaction but are enforced at the platform level. For example:
These limits are fixed across all transactions, such as:
These limits pertain to data and code size:
Limit: A single transaction can execute a maximum of 100 SOQL queries.
for (Integer i = 0; i <= 100; i++) {
Integer count = [SELECT COUNT() FROM Account];
}
This runs a SOQL query in a loop, exceeding the limit.
Move SOQL queries outside loops by using collections and batch processing.
Improved Code:
Limit: A single transaction can execute a maximum of 150 DML statements.
for (Account acc : [SELECT Id, Name FROM Account]) {
Contact con = new Contact(AccountId = acc.Id, LastName = 'Test');
insert con;
}
This inserts one record per loop iteration, quickly exceeding the 150 DML limit.
Perform bulk DML operations by using collections.
Improved Code:
List<Contact> contacts = new List<Contact>();
for (Account acc : [SELECT Id, Name FROM Account]) {
contacts.add(new Contact(AccountId = acc.Id, LastName = 'Test'));
}
insert contacts;
Limit: Total heap size for a transaction is 6 MB (synchronous) or 12 MB (asynchronous).
List<String> largeData = new List<String>();
for (Integer i = 0; i < 100000; i++) {
largeData.add('Sample Data ' + i);
}
System.debug(largeData);
This code generates a large data structure that may exceed the heap size limit.
Limits.getHeapSize() to monitor heap usage.Improved Code:
List<String> largeData = new List<String>();
for (Integer i = 0; i < 100; i++) { // Reduce iterations
largeData.add('Sample Data ' + i);
}
System.debug(largeData);
System.debug('Heap size: ' + Limits.getHeapSize());
Limits class to monitor resource usage. For example:SELECT *.Understanding and adhering to governor limits is critical for Salesforce developers. These limits:
Failure to respect governor limits results in unhandled exceptions that halt code execution, negatively impacting user experience and business processes.
Governor limits are a fundamental aspect of Salesforce’s architecture, ensuring performance, scalability, and fairness in its multitenant environment. For developers, these limits are both a challenge and a guide to writing efficient, resource-conscious code.
By understanding the different types of limits and implementing best practices like bulkification, SOQL optimization, and asynchronous processing, you can create applications that run smoothly within these constraints. Mastering governor limits not only prevents runtime exceptions but also lays the foundation for robust, scalable Salesforce solutions.