Sub topic 2
The limits that matter most in a transaction
This chapter assumes you know basic Apex syntax, SOQL, DML, and what a trigger is. It covers which governor limits cause most failures, how they are counted, and where to find current values.
What a transaction is
A transaction is one unit of work that commits or rolls back as a whole. A save from the UI, an API call, or one run of an execute method in Batch Apex each starts a transaction. Every trigger, flow, and Apex class that runs because of that save shares the same limit counters.
Limits are counted per transaction, not per trigger or per class. If a trigger on Account fires a trigger on Contact, both spend from one budget.
The limits to know first
These are the synchronous values at the time of writing. Treat them as a guide and check the Apex Developer Guide section "Execution Governors and Limits" for current numbers.
| Resource | Synchronous limit | Asynchronous limit |
|---|---|---|
| SOQL queries issued | 100 | 200 |
| Records returned by SOQL | 50,000 | 50,000 |
| DML statements issued | 150 | 150 |
| Records processed by DML | 10,000 | 10,000 |
| CPU time | 10,000 ms | 60,000 ms |
| Heap size | 6 MB | 12 MB |
Asynchronous means Queueable, Batch, and @future code. Its higher CPU, heap, and query limits are why heavy work is moved there, which a later chapter covers.
Counts versus volumes
The limits fall into two kinds, and the difference decides how you fix a problem.
- Statement counts: SOQL queries and DML statements. One query that returns 1 record and one that returns 40,000 both cost 1 against the 100-query limit. One
insertof 200 records costs 1 DML statement. - Volumes: rows returned, rows processed by DML, CPU time, and heap size. These grow with the amount of data and the work you do on it.
Counts are the usual cause of failures in triggers. A trigger receives up to 200 records at once. If it runs one query per record, a full batch uses 200 queries and fails at the 101st. The fix is to run one query for all records.
A failure you can reproduce
This trigger from the plan queries inside a loop.
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;
}
}Inserting 1 Contact works. Inserting 101 Contacts in one operation throws System.LimitException: Too many SOQL queries: 101. The whole transaction rolls back, so none of the Contacts are saved.
This is why code that passes a manual test with one record can still fail in production. A data load, an API call, or a Flow that creates records in bulk sends many records through the same trigger at once.
Limits you can't catch
A LimitException cannot be handled with try/catch. Once a limit is exceeded, the transaction ends and rolls back. You have to stay under the limits by design, and that is the purpose of bulkification.
Row limits and CPU in practice
Two volume limits often surprise people.
- The 50,000 row limit counts rows returned by all queries together. Ten queries that each return 6,000 rows exceed it, even though you issued only 10 queries.
- CPU time counts Apex execution, not database time. Nested loops over large collections consume CPU quickly. Time spent waiting for a query to run does not count.
Heap size is the memory held by your variables. Querying many records with many fields into a list can approach 6 MB. Selecting only the fields you need helps.
Where to look up values
Limits change between releases, and some depend on context. Use two sources:
- The Apex Developer Guide page "Execution Governors and Limits", for the full list.
- The
Limitsclass at runtime, which reports both usage and the maximum for the current context. A later chapter in this tutorial covers it.
Exercise
A trigger receives 150 Accounts. It runs 1 SOQL query that returns 20,000 rows, then runs 1 update on a list of 150 Accounts. How much of the synchronous SOQL query, query row, DML statement, and DML row limits does this use?
Show answer
It uses 1 of 100 queries, 20,000 of 50,000 query rows, 1 of 150 DML statements, and 150 of 10,000 DML rows. All four are well within their limits, because the code issues one statement for the whole batch instead of one per record.