Sub topic 13
Choosing the right strategy
This chapter assumes you can write a basic Apex trigger, you know that Trigger.new and Trigger.oldMap hold the records in the current batch, and you know that static variables last for one transaction. It compares the recursion guards from earlier chapters and gives you a way to pick one.
Start with the cheapest fix
Each strategy has a cost. Work down this list and stop at the first option that solves the problem.
- Use a before trigger. If you only need to change fields on the records being saved, assign to
Trigger.newin a before trigger. No DML runs, so nothing re-fires. - Check for changed fields. If the logic matters only when certain fields change, compare
Trigger.newwithTrigger.oldMapand process only those records. A re-fired trigger that sees no relevant change does nothing. - Track record Ids in a static Set. If you must run DML in an after trigger and the same records can come back, remember which Ids you already handled.
- Add a bypass switch. If some code paths need to turn the trigger logic off on purpose, put a static flag in the handler class.
A static Boolean flag is not on this list. It is the simplest guard, but it can skip records. Use it only when you know a single chunk of up to 200 records is all you will ever process.
Compare the options
| Strategy | Stops recursion by | Main risk |
|---|---|---|
| Before trigger, no DML | Avoiding the extra DML | Cannot create or update other records safely |
| Changed-field check | Ignoring records with no relevant change | Misses cases where the field changes again on purpose |
| Static Boolean flag | Running the logic once per transaction | Skips records in later 200-record chunks |
| Static `Set<Id>` | Skipping Ids already processed | Skips a record that legitimately needs a second pass |
| Bypass switch | Turning logic off explicitly | Someone forgets to turn it back on |
Combine guards when needed
The strategies work together. A common pattern in an after trigger is to filter by changed field first, then skip Ids already processed:
public class AccountTriggerHandler {
private static Set<Id> processedIds = new Set<Id>();
public static void afterUpdate(List<Account> newList, Map<Id, Account> oldMap) {
List<Account> toUpdate = new List<Account>();
for (Account acc : newList) {
Account oldAcc = oldMap.get(acc.Id);
if (acc.Rating == oldAcc.Rating) {
continue;
}
if (processedIds.contains(acc.Id)) {
continue;
}
processedIds.add(acc.Id);
toUpdate.add(new Account(Id = acc.Id, Description = 'Reviewed'));
}
if (!toUpdate.isEmpty()) {
update toUpdate;
}
}
}The changed-field check cuts down the work. The Set guarantees that each record is handled once even if the update fires the trigger again.
Check the boundaries
Static variables reset when work moves to a new transaction. Queueable jobs, future methods, and batch Apex each start with fresh static state. A guard cannot stop a trigger that enqueues a job which updates the same records again. In that case, fix the loop itself: use a changed-field check so the second run finds nothing to do.
Exercise
An after update trigger on Account updates related Contacts, and you also need to set a field on the Account itself. A colleague suggests a static Boolean flag for the whole trigger. Which approach would you choose instead, and why?
Show answer
Set the Account field in a before update trigger by assigning to Trigger.new, which needs no DML and cannot re-fire the trigger. Keep the Contact update in the after trigger, and use a changed-field check or a static Set of Ids if that update can lead back to the Account. This avoids the Boolean flag, which would skip records in later 200-record chunks of a bulk update.
Exercise
Your guard is a static Set of Ids. A queueable job started from the trigger updates the same Accounts, and the trigger runs again. Does the Set prevent the second run? What should you do?
Show answer
No. The queueable job runs in a new transaction, so the static Set starts empty. Add a changed-field check so the trigger ignores records with no relevant change, or avoid updating records that already hold the target values.