Skip to content
Namaste Salesforce Namaste Salesforce
Developer Roadmap

Governor limits

The multitenant platform enforces per-transaction limits. Know the key ones and the bulkification mindset they demand.

Developer Roadmap Article 1 min

Governor limits

Salesforce is multitenant: your code shares infrastructure with thousands of other customers. To stop any one tenant from monopolising resources, the platform enforces governor limits — hard ceilings on what a single transaction may do. Understanding them is not a footnote; it is the central design constraint of all Apex, and violating one throws an uncatchable LimitException that rolls the whole transaction back.

The limits you will bump into most are per-transaction. The headline ones include 100 SOQL queries, 150 DML statements, 50,000 records retrieved by SOQL, 10 seconds of CPU time (on synchronous transactions), and 6 MB of heap. Async contexts like Batch and Queueable get higher ceilings, which is part of why they exist.

The behaviour that causes limit failures is almost always the same anti-pattern: doing per-record work inside a loop. A query or a DML statement inside a for loop over 200 records is 200 queries or 200 DML statements — instantly over the limit. The fix is bulkification: operate on collections, query once for everything you need, and perform one DML per object type.

// BAD: a SOQL query and DML inside the loop
for (Account a : accounts) {
    List<Contact> cs = [SELECT Id FROM Contact WHERE AccountId = :a.Id]; // query per record!
    // ...
    update cs; // DML per record!
}

// GOOD: one query, one DML, set-based
Set<Id> acctIds = new Map<Id, Account>(accounts).keySet();
List<Contact> allContacts =
    [SELECT Id, AccountId FROM Contact WHERE AccountId IN :acctIds];
// ... process allContacts in memory ...
update allContacts; // a single bulk DML

Internalise one rule and you will avoid most limit problems: never put SOQL or DML inside a loop. Query before the loop, collect changes in a list during the loop, and DML once after it. Everything in the Apex section that follows is written with this mindset. Monitor your limits at runtime with Limits.getQueries() and friends while learning. With the platform's constraints understood, the next section dives into the Apex language itself.

Advertise with us · 728×90