Sub topic 11
Bypass switches for deliberate re-entry
This chapter assumes you can write an Apex trigger, you know that static variables last for one transaction, and you have seen a handler class that holds a recursion guard. Guards stop a trigger from re-entering itself. Sometimes you want the opposite: a deliberate second pass, or a way to switch the trigger logic off for one piece of code.
Why a guard can get in the way
A recursion guard is blunt. It skips the logic whenever the trigger fires again, and it can't tell an accidental re-fire from one you want. Two common cases:
- A data-cleanup routine updates thousands of accounts and must not run the trigger logic on each one.
- A trigger's own update changes a field, and a second pass over those records is needed to finish the work.
A bypass switch is a static flag that the handler checks before it does anything. Code you control sets the flag, runs its DML, and clears the flag.
A bypass switch in the handler
Put the switch in the same handler class that holds the guard, so every check lives in one place.
public class AccountTriggerHandler {
private static Boolean bypass = false;
private static Set<Id> processedIds = new Set<Id>();
public static void setBypass(Boolean value) {
bypass = value;
}
public static void clearProcessed(Set<Id> ids) {
processedIds.removeAll(ids);
}
public static void afterUpdate(List<Account> newList) {
if (bypass) {
return;
}
List<Account> toUpdate = new List<Account>();
for (Account acc : newList) {
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 trigger stays a thin router, as in the earlier chapter:
trigger AccountTrigger on Account (after update) {
if (Trigger.isAfter && Trigger.isUpdate) {
AccountTriggerHandler.afterUpdate(Trigger.new);
}
}Using the switch
Wrap the DML in a try/finally block. If the DML throws, the finally block still clears the flag, so later code in the same transaction is not silently skipped.
List<Account> accounts = [SELECT Id, Rating FROM Account LIMIT 200];
for (Account acc : accounts) {
acc.Rating = 'Warm';
}
AccountTriggerHandler.setBypass(true);
try {
update accounts;
} finally {
AccountTriggerHandler.setBypass(false);
}Deliberate re-entry
To let the trigger process a record a second time, remove its Id from the processed set before the next DML. The clearProcessed method above does that.
Set<Id> ids = new Map<Id, Account>(accounts).keySet();
AccountTriggerHandler.clearProcessed(ids);
update accounts; // the handler will process these records againUse this sparingly. Each re-entry runs the trigger again, so it counts toward the trigger depth limit and your governor limits.
Risks to watch for
- Hidden skipped logic. Validation or rollup code in the handler is skipped too. Bypass only when that is acceptable.
- No reset across transactions. Static state resets by itself when the transaction ends, so you never need to reset it for the next request. You only reset it within the current transaction.
- Access control. Any class can call a public
setBypass. If that worries you, make the setter@TestVisible privateor expose it through a narrower method. - Asynchronous work. A queueable or batch job starts a new transaction with fresh static state, so a bypass set before enqueuing does not carry over.
Exercise
A data-cleanup method calls AccountTriggerHandler.setBypass(true), runs an update that throws an exception, and the exception is caught by the caller. The method has no try/finally. What goes wrong for later code in the same transaction, and how do you fix it?
Show answer
The bypass flag stays true, so every later Account update in that transaction skips the handler logic without any error. Fix it by putting the update in a try block and calling setBypass(false) in the finally block, so the flag is reset whether or not the DML succeeds.