Sub topic 10
Keeping guards in a trigger handler class
This chapter assumes you can write a basic Apex trigger, you know what a static variable is, and you have seen the static Set<Id> guard from the previous chapter. Here you move the guard out of the trigger and into a handler class.
Why move logic out of the trigger
A trigger body is hard to test and hard to reuse. If you put the guard directly in the trigger, you cannot declare a static variable there, because a trigger cannot hold static members. The guard needs a home that lives for the whole transaction. A handler class is that home.
The pattern has two parts:
- The trigger decides which event fired and calls the handler.
- The handler holds the logic and the static guard state.
The trigger only routes
The trigger contains no business logic. It checks the context and passes the data on.
trigger AccountTrigger on Account (after update) {
if (Trigger.isAfter && Trigger.isUpdate) {
AccountTriggerHandler.afterUpdate(Trigger.new);
}
}When Salesforce re-fires this trigger inside the same transaction, the call reaches the same handler class. The handler's static state is still there, so it can recognize records it already handled.
The handler owns the guard
The handler keeps a static Set<Id> of processed records. Each call skips Ids it has seen and processes only new ones.
public class AccountTriggerHandler {
private static Set<Id> processedIds = new Set<Id>();
public static void afterUpdate(List<Account> newList) {
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;
}
}
}Follow what happens when one Account is updated:
- The trigger fires and calls
afterUpdate. - The Id is not in
processedIds, so the handler adds it and updates the record. - The update re-fires the trigger, which calls
afterUpdateagain. - The Id is now in the set, so
toUpdatestays empty and no DML runs. - The recursion stops after one extra pass.
Combining guards in one place
The handler is also where you combine strategies. A changed-field check keeps the logic limited to records that matter, and the Id set stops repeat work. Both live in one class, so a reader sees the full recursion policy in one place.
public static void afterUpdate(List<Account> newList, Map<Id, Account> oldMap) {
List<Account> changed = new List<Account>();
for (Account acc : newList) {
Account oldAcc = oldMap.get(acc.Id);
if (acc.Rating != oldAcc.Rating) {
changed.add(acc);
}
}
// process only the records in 'changed'
}If you use both, run the changed-field check first, then record the Id in the set only for records you actually process. Adding an Id for a record you skipped would block a later legitimate change to it in the same transaction.
Benefits and limits
| Benefit | Detail |
|---|---|
| One place for the guard | You change the policy in one class, not in each trigger. |
| Testable | Test classes call the handler or perform DML and check the result. |
| Reusable | Several triggers or services can call the same handler method. |
The handler does not remove the need for a guard. It only organizes it. Static state still resets between transactions, so the guard protects one transaction only.
Exercise
Why can the static Set<Id> live in the handler class but not in the trigger itself, and what does the set do when the trigger re-fires?
Show answer
A trigger cannot declare static members, so the guard must live in a class. The handler's static set keeps its values for the whole transaction. When the trigger re-fires, the handler finds the Ids already in the set, skips those records, and issues no further DML.