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:

Efficient and optimized code
Bulk-safe design
Item text
Predictable performance in production environments

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 ContextLimit
Synchronous Apex100 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 TypeValue
DML Statements150 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.

ContextLimit
Synchronous Apex10 seconds
Asynchronous Apex60 seconds

Real-World Causes of CPU Limit Errors

Nested loops
Inefficient logic
Large string operations
Item text
Unnecessary calculations

How to Avoid CPU Issues

Replace nested loops with maps
Reduce redundant logic
Move heavy processing to Queueable or Batch Apex
Avoid unnecessary iterations

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.

ContextLimit
Synchronous Apex6 MB
Asynchronous Apex12 MB

Common Heap Issues

Storing large JSON responses in memory
Holding large collections unnecessarily
Processing files or blobs in triggers

Best Practices

Process data in smaller chunks
Avoid keeping unused variables in memory
Use Batch Apex for large datasets

5. Callout Limits

Salesforce allows a maximum of 100 callouts per transaction.

Important Rules

Never make callouts directly from triggers
Never perform callouts inside loops

Correct Design

Use @future(callout=true) or Queueable Apex
Aggregate data before making callouts
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.

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *