Sub topic 1
Why Governor Limits Exist
Prerequisites
This chapter assumes you can read basic Apex: classes, methods, loops and conditionals. It also assumes you know the List, Set and Map collections, simple SOQL queries, and DML statements such as insert, update and delete. You should know that an Apex trigger is code that runs before or after a record is saved.
Multitenancy in one paragraph
Salesforce runs many customers (called tenants) on the same shared infrastructure. Your org's data and code are separate from other orgs, but they use the same servers, databases and application layer. This design is called multitenancy. It keeps costs and upgrade effort low for everyone, but it has one consequence for developers: one org's code must not be able to slow down or crash the platform for the others.
What governor limits are
Governor limits are caps that the Apex runtime enforces on each transaction. A transaction is one unit of work, such as saving a record together with every trigger, flow and class it fires. The runtime counts the resources the transaction uses. If any count passes its cap, the runtime throws a limit exception that you cannot catch, and the whole transaction is rolled back.
The limits cover the resources that a runaway piece of code could otherwise monopolize:
| Resource | What it protects | Example limit (synchronous) |
|---|---|---|
| SOQL queries | Database load | 100 per transaction |
| DML statements | Database write load | 150 per transaction |
| Rows returned by SOQL | Memory and database load | 50,000 per transaction |
| CPU time | Application server processing | 10,000 ms |
| Heap size | Server memory | 6 MB |
These numbers are the commonly documented synchronous values. Salesforce can change them, and asynchronous Apex gets higher caps for some of them. Always check the current Apex Developer Guide section on execution governors and limits before relying on a specific number.
Why limits matter more than they first appear
A limit is usually not a problem when you test with one record. It becomes a problem when the same code receives many records at once. Data loads, integrations and bulk edits all send records in batches, and a trigger may receive up to 200 records in one call.
Consider this trigger. It runs one query for each Contact.
trigger ContactBefore on Contact (before insert) {
for (Contact c : Trigger.new) {
// Anti-pattern: one query per record
Account a = [SELECT Id, Industry FROM Account WHERE Id = :c.AccountId];
c.Description = 'Industry: ' + a.Industry;
}
}With 1 record, it uses 1 query. With 101 or more records in the same transaction, it needs more than 100 queries, and the transaction fails. The code looks correct and passes a manual test, then fails the first time someone imports a spreadsheet. This is the main reason bulkified code matters: the limits are per transaction, not per record.
The limit also applies across everything in the transaction. If your trigger uses 60 queries and a managed package trigger on the same object uses 50, the total is 110 and the transaction fails. Code you wrote correctly in isolation can still fail when combined with other automation.
How a transaction flows
The diagram shows how the runtime counts resources and decides what happens next.
Common errors at this stage
- Testing only with one record. A trigger that works for a single save can fail for a batch. Always think about 200 records.
- Counting limits per class instead of per transaction. Triggers, flows, workflow updates and packages all share one set of counters.
- Wrapping the failing code in try/catch. Limit exceptions bypass catch blocks, so this hides nothing and fixes nothing.
- Hard-coding limit values from memory. Limits change between releases. Look up the current numbers, or read them in code with the
Limitsclass, which later chapters cover.
What you will do next
The rest of this tutorial builds on one idea: write code whose resource use does not grow with the number of records. You will learn which limits cause most failures, how to spot queries and DML inside loops, and how to replace them with Sets, Maps and Lists.
Exercise
A trigger on Contact runs one SOQL query inside a for loop over Trigger.new. It works when you save one Contact in the UI. A user then imports 150 Contacts in a single batch and the import fails. Why does it fail, and why can't a try/catch block around the query prevent the failure?
Show answer
The batch of 150 Contacts runs in one transaction, so the loop runs the query 150 times. That passes the limit of 100 SOQL queries per transaction, and the runtime throws a limit exception. Governor limit exceptions cannot be caught, so try/catch does not stop them, and the whole transaction is rolled back. The fix is to remove the query from the loop, which later chapters show.
Exercise
Why does Salesforce enforce governor limits, and why might code that stays under the limits on its own still fail in a real org?
Show answer
Salesforce is a multitenant platform where many orgs share the same resources, so limits stop one transaction from using too much and harming others. Limits are counted per transaction, not per piece of code. Triggers, flows and installed packages that run in the same transaction share the same counters, so their combined use can exceed a limit even when each part is under it alone.