Sub topic 8
Why a Boolean flag can skip records beyond 200
This chapter assumes you can write a simple Apex trigger, know List, Set and Map, and understand that a static variable keeps its value for one transaction. It also assumes you know the Boolean flag guard from the previous chapter. Here you will see where that guard breaks.
The flag guard in brief
A static Boolean flag stops a trigger from re-entering its own logic. The first call sets the flag to true. Any later call in the same transaction sees the flag and returns early.
public class AccountTriggerHandler {
private static Boolean hasRun = false;
public static void afterUpdate(List<Account> newList) {
if (hasRun) {
return;
}
hasRun = true;
List<Account> toUpdate = new List<Account>();
for (Account acc : newList) {
toUpdate.add(new Account(Id = acc.Id, Description = 'Reviewed'));
}
update toUpdate;
}
}With a few records this works. The update fires the trigger again, the flag is already true, and the second call returns at once.
What chunking does
Salesforce does not hand a large DML operation to a trigger in one piece. It splits the records into chunks of up to 200 and runs the trigger once per chunk. All chunks belong to the same transaction.
Suppose you update 450 accounts. The trigger runs three times:
| Trigger call | Records in Trigger.new | Flag on entry |
|---|---|---|
| 1 | 200 | false |
| 2 | 200 | true |
| 3 | 50 | true |
Static variables last for the whole transaction, not for one chunk. The first call sets hasRun to true. Calls 2 and 3 see true and return without touching their records. Only 200 of the 450 records get Description = 'Reviewed'. The other 250 are skipped, and no error tells you so.
Why this bug is easy to miss
Most tests and manual checks touch only a few records, which fit in one chunk. The flag then behaves correctly. The failure shows up only with bulk operations such as data loads, API calls, or batch jobs that update more than 200 records at once. By then the skipped records are in production.
The fix in outline
The guard needs to remember which records were handled, not just that the trigger ran. If you track record Ids, a re-fired trigger skips records it already processed, and a new chunk still processes its own records. The next chapter builds that guard with a static Set<Id>.
Checking your own code
Review any trigger or handler that uses a static Boolean to prevent recursion. Ask these questions:
- Can this code run for more than 200 records in one transaction?
- If so, what happens to records in the second and later chunks?
- Does a test update at least 201 records and assert on every one of them?
Exercise
A data load updates 600 Account records in one transaction. The after update trigger uses a static Boolean hasRun guard as shown above. How many trigger calls occur for the load, and how many records are processed by the guarded logic?
Show answer
The 600 records are split into three chunks of 200, so the trigger runs three times in the first pass. Only the first call finds hasRun false, so only its 200 records are processed. The other 400 are skipped silently, because the flag stays true for the rest of the transaction. (The handler's own update also re-fires the trigger, but that call returns early too.)