Sub topic 7
The static Boolean flag guard
This chapter assumes you can write a basic Apex trigger, know what a static variable is, and have seen the "maximum trigger depth exceeded" error. The earlier chapters covered why a trigger that runs DML on its own object fires again. Here you will stop that with a static Boolean flag.
The idea
A static variable keeps its value for the whole transaction. The first time the trigger logic runs, it sets a flag to true. When the DML inside that logic fires the trigger again, the logic sees the flag and returns early.
Put the flag in a handler class, not in the trigger body. Trigger bodies can't declare static variables.
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;
}
}The trigger only routes to the handler:
trigger AccountTrigger on Account (after update) {
if (Trigger.isAfter && Trigger.isUpdate) {
AccountTriggerHandler.afterUpdate(Trigger.new);
}
}What happens at run time
The sequence for one user update:
- The user updates an Account. The trigger fires and
hasRunisfalse. - The handler sets
hasRun = true, then runsupdate toUpdate. - That update fires the trigger a second time.
hasRunis nowtrue, so the handler returns. - The transaction finishes with one pass of your logic and no depth error.
Why the flag is risky
The flag is simple, but it is blunt. It says "this logic has run once in this transaction." It does not say which records were processed.
Salesforce processes bulk DML in chunks of up to 200 records. Suppose a data load updates 600 Accounts. The trigger fires once per chunk, all in one transaction:
| Trigger call | Records in chunk | `hasRun` on entry | Result |
|---|---|---|---|
| 1 | 1 to 200 | false | Processed |
| 2 | 201 to 400 | true | Skipped |
| 3 | 401 to 600 | true | Skipped |
Only the first 200 records get your logic. The flag can't tell a re-fired call from a legitimate later chunk. There is no error, so the bug often shows up only in production data loads.
When the flag is acceptable
The flag is reasonable when all of these hold:
- The logic is meant to run once per transaction, such as a single summary calculation.
- The trigger never handles more than 200 records in one transaction.
- You understand that a second legitimate change in the same transaction will be ignored.
Even then, check whether a before trigger or a changed-field check removes the need for a guard.
Common mistakes
- Declaring the flag in the trigger body. Trigger bodies can't hold static members. Use a class.
- Setting the flag after the DML. The re-fired trigger runs inside the
updatestatement, so the flag is stillfalseand the loop continues. Set it before the DML. - Using one flag for several events. A flag set during
afterInsertalso blocksafterUpdatelogic in the same transaction. Use one flag per event, or switch to an Id set.
Exercise
A data loader updates 450 Accounts in one transaction, and your after update handler uses a static Boolean hasRun. How many Accounts get the handler's logic, and why?
Show answer
Only the first 200. Salesforce fires the trigger once per chunk of up to 200 records. The first call sets hasRun to true. The calls for records 201 to 400 and 401 to 450 see the flag and return early without error.