Sub topic 6
How long static state lives: the transaction
This chapter assumes you know basic Apex syntax, how static and instance members differ, and that Apex code runs inside Salesforce transactions. It explains how long a static variable keeps its value and what that means for the code you write.
Static state belongs to the transaction
In Apex, a static variable is not global to the org and does not persist between requests. Its value lives for one transaction. A transaction is one unit of work, such as a single DML save with its triggers, one web service call, or one execution of a batch execute chunk. When the transaction ends, all static variables are discarded. The next transaction starts again with the initial values.
Within the transaction, every piece of code that touches the class sees the same static value. A trigger, a flow-invoked Apex action, and a helper class all share it.
A recursion guard
The most common use is stopping the same logic from running twice. A save can fire a trigger, which updates records, which fires the trigger again. A static flag breaks the loop.
public class TriggerGuard {
private static Boolean hasRun = false;
public static Boolean shouldRun() {
if (hasRun) {
return false;
}
hasRun = true;
return true;
}
}
// Usage inside a trigger handler
if (TriggerGuard.shouldRun()) {
// do the work once per transaction
}The first call returns true and flips the flag. Every later call in the same transaction returns false. A new transaction sees hasRun as false again.
Common errors
- Expecting values to carry over. Setting a static variable in one request and reading it in the next does not work. If you need data to survive, store it in a record, a custom setting, or the platform cache.
- Using a single Boolean for bulk work. A trigger can receive up to 200 records in one batch, and a bulk operation may call the trigger more than once in a transaction, with different records each time. A flag that blocks every call after the first can skip records that were never processed. Track a
Set<Id>of processed record IDs instead. - Assuming a flag resets mid-transaction. It does not. If the guard must allow a second pass, you have to reset it in code.
- Forgetting that tests share one transaction. In a test method, the setup and the code under test run in the same transaction unless you use
Test.startTest()andTest.stopTest(). A guard set during setup can silently block the code you are testing.
public class RecordGuard {
private static Set<Id> processed = new Set<Id>();
public static Boolean markIfNew(Id recordId) {
return processed.add(recordId);
}
}Set.add returns true only when the value was not already in the set. Each record is therefore handled once, and records that appear for the first time on a later call still get processed.
Instance state compared
An instance variable lives as long as its object. Because objects are created inside a transaction, they also disappear when it ends. The difference is scope within the transaction: instance state is private to one object, while static state is shared by all code that uses the class.
Exercise
A trigger on Account uses a static Boolean flag to run its logic only once. A user bulk-loads 600 accounts through the Data Loader, which sends them in batches of 200. Does the flag block the second batch, and why?
Show answer
No. Each batch of 200 is processed in its own transaction, and static variables reset at the end of every transaction. The flag only blocks repeat calls inside one batch's transaction. Within a single transaction that calls the trigger more than once, a Boolean flag could skip records, so a Set of processed record IDs is safer.