Sub topic 6
Static variables live for one transaction
This chapter assumes you can write a simple Apex trigger, know List, Set, and Map, and understand DML statements. Guards against recursion depend on one language feature: the static variable. You need to know exactly how long it lives.
What static means in Apex
A static variable belongs to the class, not to an instance of it. Apex keeps one copy of it, and every piece of code in the transaction that reads it sees the same value.
A transaction is one unit of work in Salesforce. It starts when a request begins, such as a record save, an API call, or a test method. It ends when the work commits or rolls back. When it ends, Apex discards every static variable. The next transaction starts again from the initial value.
public class Counter {
public static Integer calls = 0;
public static void hit() {
calls++;
}
}If a trigger calls Counter.hit() twice in one transaction, Counter.calls is 2. In a new transaction it starts at 0 again.
Why this helps with recursion
When an after trigger updates records, the update fires the same trigger again inside the same transaction. Both runs of the trigger share the same static variables. The first run can write a value, and the second run can read it and decide to stop.
A normal local variable cannot do this. Each run of the trigger creates its own local variables, so the second run never sees what the first run stored.
The diagram below shows the sequence.
Scope: what is shared and what is not
| Situation | Same static values? |
|---|---|
| Trigger re-fired by DML in the same transaction | Yes |
| Several 200-record chunks of one bulk operation | Yes |
| Different triggers and classes in the same transaction | Yes, if they use the same class |
| A separate user save or API call | No, it is a new transaction |
| A queueable, future, or batch job started by the code | No, it runs in its own transaction |
| Each test method | No, each starts fresh |
The second row matters. The chunks of one large operation all run in the same transaction, so a static variable set while processing the first 200 records is still set when the trigger runs for the next 200. The next chapters build on this.
Common errors
- Declaring the flag as a local or instance variable. A local variable inside the trigger or the handler method is recreated on every run, so it never stops recursion. Declare the guard
staticat class level. - Putting a static variable in a trigger file. Triggers cannot declare static members. Put the variable in a class and call that class from the trigger.
- Assuming it persists across transactions. A static variable cannot remember that a record was handled yesterday or in the previous request. Use a field on the record for that.
- Expecting it to carry into asynchronous work. A queueable job starts with fresh static values.
Check the behavior yourself
This test shows that one transaction shares the value, and that the value does not leak into another test.
@IsTest
private class StaticScopeTest {
@IsTest
static void valueIsSharedInsideOneTransaction() {
Counter.hit();
Counter.hit();
System.assertEquals(2, Counter.calls);
}
@IsTest
static void eachTestStartsFresh() {
System.assertEquals(0, Counter.calls);
}
}Exercise
A developer declares Boolean hasRun = false; inside the handler method, sets it to true, then updates records. The update fires the trigger again, and the trigger still recurses. Why, and what is the fix?
Show answer
A variable declared inside a method is local, so each run of the method gets its own copy that starts at false. The second run never sees the true value. Declare it as private static Boolean hasRun = false; at class level so all runs in the transaction share one copy.
Exercise
Will a static Set of record Ids filled during a trigger still hold those Ids when a queueable job started by that trigger runs? Explain.
Show answer
No. The queueable job runs in its own transaction, and Apex resets static variables for each new transaction. The job sees an empty Set.