
Salesforce Governor Limits Explained with Real-Time Scenarios
Salesforce Governor Limits are system-enforced limits that control how much data, memory, and processing power a single transaction can consume. Since Salesforce operates on a multi-tenant architecture, these limits ensure that no single organization or piece of code negatively affects the performance of others on the platform.
For Salesforce developers, understanding governor limits is not optional — it is a core skill. Whether you are writing Apex triggers, batch classes, Lightning Web Components, or integrations, governor limits influence every design decision you make. In this blog, we will break down the most important Salesforce Governor Limits using real-time scenarios, correct examples, and proven best practices, so you not only avoid common runtime errors but also learn how to write clean, efficient, and production-ready Apex code.
What Are Salesforce Governor Limits?
Salesforce Governor Limits are runtime limits enforced by the Salesforce platform to control how much data and processing power a single transaction can consume. These limits apply to Apex code, triggers, integrations, and asynchronous processes.
Because Salesforce follows a multi-tenant architecture, resources are shared among thousands of organizations. Governor Limits ensure platform stability by preventing poorly written or resource-heavy code from impacting other customers.
In simple terms, governor limits define how much you can do in one execution context. If your code exceeds any of these limits, Salesforce immediately stops the transaction and rolls back all changes.
Why Salesforce Enforces Governor Limits
Governor Limits exist to maintain fairness and reliability across the platform. Without them, one organization could consume excessive resources, leading to performance degradation for others.
From a developer’s perspective, governor limits encourage:
Experienced Salesforce developers design solutions with governor limits in mind, rather than trying to work around them.
Most Important Salesforce Governor Limits for Developers
Not every limit causes problems in real projects. Below are the limits that most commonly impact Salesforce developers.
1. SOQL Query Limits
Salesforce restricts the number of SOQL queries that can be executed in a single transaction.
| Execution Context | Limit |
| Synchronous Apex | 100 SOQL queries |
| Asynchronous Apex (Batch, Queueable, Future) | 200 SOQL queries |
Real-Time Scenario
A very common mistake is writing SOQL queries inside loops, especially in triggers.
for (Account acc : Trigger.new) {
List<Contact> contacts = [
SELECT Id FROM Contact WHERE AccountId = :acc.Id
];
}This works fine for one record, but fails when multiple records are processed. If more than 100 records are involved, Salesforce throws a Too many SOQL queries error.
Correct Approach
Always query data once and process it using collections.
Set<Id> accountIds = new Set<Id>();
for (Account acc : Trigger.new) {
accountIds.add(acc.Id);
}
Map<Id, List<Contact>> contactsByAccount = new Map<Id, List<Contact>>();
for (Contact con : [
SELECT Id, AccountId FROM Contact WHERE AccountId IN :accountIds
]) {
if (!contactsByAccount.containsKey(con.AccountId)) {
contactsByAccount.put(con.AccountId, new List<Contact>());
}
contactsByAccount.get(con.AccountId).add(con);
}This approach is bulk-safe and works reliably in production.
2. DML Statement Limits
Salesforce limits the number of DML operations per transaction.
| Limit Type | Value |
| DML Statements | 150 per transaction |
Real-Time Scenario
Performing DML operations inside loops is one of the fastest ways to hit governor limits.
for (Contact con : contacts) {
update con;
}If the list contains more than 150 records, the transaction fails.
Best Practice
Always perform DML operations on collections.
update contacts;This improves performance and ensures scalability.
3. Apex CPU Time Limits
CPU time represents the total amount of processing time your Apex code consumes.
| Context | Limit |
| Synchronous Apex | 10 seconds |
| Asynchronous Apex | 60 seconds |
Real-World Causes of CPU Limit Errors
How to Avoid CPU Issues
CPU limit errors usually indicate design problems, not data issues.
4. Heap Size Limits
Heap size is the amount of memory Apex can consume during execution.
| Context | Limit |
| Synchronous Apex | 6 MB |
| Asynchronous Apex | 12 MB |
Common Heap Issues
Best Practices
5. Callout Limits
Salesforce allows a maximum of 100 callouts per transaction.
Important Rules
Correct Design
Monitoring Governor Limits During Execution
Salesforce provides built-in methods to monitor governor limits.
Limits.getQueries();
Limits.getLimitQueries();
Limits.getCpuTime();
Limits.getLimitCpuTime();These methods are useful during debugging and performance tuning.
Common Interview Question
Why does Salesforce enforce governor limits?
Salesforce enforces governor limits to protect shared resources in a multi-tenant environment and ensure consistent performance for all organizations.
This answer is technically correct and expected in interviews.
Conclusion
Salesforce Governor Limits play a crucial role in building reliable and scalable applications. Developers who understand these limits early in their careers write better code, avoid common runtime errors, and perform confidently in interviews.
Instead of treating governor limits as restrictions, treat them as best-practice boundaries that guide you toward clean, efficient, and production-ready Salesforce development.
At SFDXMetric, our goal is to help developers learn Salesforce the right way — through real-world scenarios, clear explanations, and practical examples.
