Sub topic 9
Tracking processed records with a static Set of Ids
This chapter assumes you can write an Apex trigger, use List, Set and Map, and know that DML (insert, update, delete statements) inside an after trigger can fire that trigger again. It also assumes you know that a static variable keeps its value for the whole transaction.
The problem with a single flag
A static Boolean guard stops recursion, but it is blunt. Salesforce splits a bulk operation into chunks of up to 200 records. All chunks run in the same transaction, so a static Boolean set during the first chunk is still true during the second. The second chunk returns early and its records are never processed.
A guard should skip only records that were already handled, not every record that arrives later.
The idea
Keep a static Set<Id> of record Ids that the logic has processed. On each run:
- Look at each record in the incoming list.
- If its Id is already in the set, skip it.
- Otherwise add the Id to the set and process the record.
A re-fired trigger receives the same records, so every Id is already in the set and nothing happens. A later chunk contains new records, so their Ids are not in the set and they are processed normally.
The handler
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;
}
}
}The trigger only routes to the handler:
trigger AccountTrigger on Account (after update) {
if (Trigger.isAfter && Trigger.isUpdate) {
AccountTriggerHandler.afterUpdate(Trigger.new);
}
}Follow one record through the transaction:
- The user updates account
001A. The trigger fires and callsafterUpdate. 001Ais not in the set, so it is added and queued for the update.- The
update toUpdatestatement fires the trigger a second time. 001Ais in the set, so it is skipped.toUpdateis empty and no DML runs.- The recursion ends after one extra pass.
Trade-offs
The Set of Ids is more precise than a Boolean, but it has limits.
- Each record runs once per transaction. If a later step legitimately changes the same record and your logic needs to run again, the guard blocks it. Combine the set with a changed-field check, or use a bypass switch, when you need deliberate re-entry.
- Memory grows with the record count. Each Id adds to heap usage. For typical volumes this is small, but a very large bulk operation grows the set accordingly.
- State does not cross async boundaries. Queueable, future and batch jobs start new transactions with an empty set.
- Only the guarded logic is skipped. Other triggers on the object still run.
Exercise
A user loads 450 accounts in one bulk update. Your after update trigger uses a static Boolean hasRun. Only the first 200 accounts get the "Reviewed" description. Why, and how does a static Set<Id> fix it?
Show answer
Salesforce processes the 450 records in chunks (200, 200 and 50) within one transaction. The static Boolean is set to true during the first chunk, so the trigger returns early for the second and third chunks and their records are skipped. A static Set<Id> skips only Ids it has already seen. The records in the later chunks have new Ids, so they are processed once each, while a re-fired trigger on the same records is still stopped.