Sub topic 15
Governor Limits Are Shared Across the Transaction
This chapter assumes you can write a basic Apex trigger, know what DML is, and have read the earlier chapters on the order of execution. Every step of a save runs in one transaction. That means every step draws from the same pool of governor limits.
One budget for the whole save
Governor limits cap the resources one transaction can use. A few that matter most here are:
| Limit (synchronous) | Value |
|---|---|
| SOQL queries | 100 |
| DML statements | 150 |
| Records processed by DML | 10,000 |
| CPU time | 10,000 ms |
Your trigger does not get its own allowance. Before triggers, after triggers, flows, workflow field updates, and any trigger that fires on a related record all count against the same totals. If a flow uses 40 queries and your trigger uses 70, the transaction fails on the 101st query.
Why triggers firing again costs more
A workflow field update can make before and after update triggers run a second time. Each run spends queries and DML again. Recursion, covered in the next chapter, multiplies this cost.
Updating a parent record in an after trigger can fire the parent's triggers too. Those also use your shared budget.
Bulk-safe patterns
A trigger receives up to 200 records at a time. A query or DML statement inside a loop runs once per record, so it uses up the limits quickly. Collect the data first, then run one query or one DML statement.
trigger ContactCountry on Contact (before insert, before update) {
Set<Id> accountIds = new Set<Id>();
for (Contact c : Trigger.new) {
if (c.AccountId != null) {
accountIds.add(c.AccountId);
}
}
Map<Id, Account> accounts = new Map<Id, Account>(
[SELECT Id, BillingCountry FROM Account WHERE Id IN :accountIds]
);
for (Contact c : Trigger.new) {
Account a = accounts.get(c.AccountId);
if (a != null) {
c.MailingCountry = a.BillingCountry;
}
}
}This trigger runs one query for any batch of up to 200 contacts. It changes Trigger.new directly, so it needs no DML.
Checking usage while you work
The Limits class reports consumption inside the transaction, as the next example shows.
System.debug('SOQL used: ' + Limits.getQueries() + ' of ' + Limits.getLimitQueries());
System.debug('DML used: ' + Limits.getDmlStatements() + ' of ' + Limits.getLimitDmlStatements());Debug logs also show a limits summary at the end of each transaction. Use it to see how much budget other automation already spent.
When the limit is hit
Exceeding a limit throws an uncatchable LimitException. The whole transaction rolls back, as with any other error. No part of the save is committed.
Exercise
A before trigger uses 60 SOQL queries and a record-triggered flow on the same save uses 45. What happens, and why?
Show answer
The transaction fails with a limit exception on the 101st query and rolls back completely. The trigger and flow share one transaction, so their queries add up against the single limit of 100.
Exercise
Why does moving a SOQL query out of a for loop help with limits?
Show answer
A query inside the loop runs once per record, up to 200 times per batch. One query before the loop, using a set of Ids, runs once for the whole batch.