Sub topic 14
Preventing recursive trigger execution
This chapter assumes you can write a basic Apex trigger, run DML (data manipulation language) statements, and read Trigger.new and Trigger.old. It also assumes you know that governor limits cap what one transaction can do.
Why triggers recurse
A trigger runs when a DML operation touches its object. If the trigger's own logic updates that same object, the update fires the trigger again. The second run may update the object again, and the cycle continues.
Salesforce stops the cycle only when the transaction hits a limit. The most common one is the maximum trigger depth of 16, which ends the transaction with an error. A loop that runs 16 levels deep also burns through SOQL and DML limits first.
Recursion can also be indirect. An Account trigger updates a Contact, a Contact trigger updates the Account, and the two keep calling each other.
The static variable guard
A static variable in Apex keeps its value for the whole transaction. A trigger can use that to remember that it has already run.
public class TriggerGuard {
private static Boolean hasRun = false;
public static Boolean shouldRun() {
if (hasRun) {
return false;
}
hasRun = true;
return true;
}
}
trigger AccountRecursionTrigger on Account (after update) {
if (!TriggerGuard.shouldRun()) {
return;
}
// Logic that might update Accounts again goes here.
}The first call to shouldRun() flips the flag and returns true. Any later call in the same transaction returns false, so the trigger exits before it does any work. The class is separate from the trigger because a trigger cannot declare static members of its own.
A gotcha with bulk batches
Salesforce processes large DML operations in batches of up to 200 records. A trigger fires once per batch, and all batches in one transaction share the same static variables.
Suppose you insert 500 records. The trigger runs three times: 200, 200, then 100 records. With the guard above, only the first batch of 200 runs the logic. The other 300 records are skipped silently.
Two things can fix this. The first is to track record Ids instead of a single flag:
public class TriggerGuard {
private static Set<Id> processedIds = new Set<Id>();
public static List<Account> filterUnprocessed(List<Account> accounts) {
List<Account> toProcess = new List<Account>();
for (Account a : accounts) {
if (!processedIds.contains(a.Id)) {
processedIds.add(a.Id);
toProcess.add(a);
}
}
return toProcess;
}
}The trigger then works only on the records the method returns. A record that comes back through a recursive update is skipped, and new records in later batches still run.
The second fix is to use the guard only when the extra update is not meant to be reprocessed, and to compare old and new values instead. If your logic runs only when a specific field changed, as shown in the oldMap chapter, a recursive update that leaves that field alone does nothing.
Choosing an approach
| Approach | Good for | Risk |
|---|---|---|
| Static Boolean | Logic that must run once per transaction | Skips later batches of 200 |
| Static Set of Ids | Per-record "already handled" tracking | Skips records that need a second pass |
| Field-change check | Logic tied to one field | Does not stop unrelated recursion |
The best fix is often to avoid the extra update. If the change can be made in a before trigger by editing Trigger.new, no DML is needed and no recursion occurs.
Exercise
You insert 450 Account records in one DML statement, and the trigger uses the static Boolean guard shown above. How many records have their logic run, and why?
Show answer
Only the first 200. The trigger fires once per batch of up to 200 records (200, 200, 50). The first call sets hasRun to true, and the static variable keeps that value for the whole transaction, so the second and third batches return early. Tracking processed record Ids avoids this.