Sub topic 2
Order of execution and why triggers can run twice
This chapter assumes you can write a basic Apex trigger, know the Trigger.new and Trigger.old context variables, and understand DML statements and the one-transaction model. Here you will see where triggers sit in a save, and which steps can make them run again.
One save, many steps
When you save a record, Salesforce does not just run your trigger. It runs a fixed sequence of steps inside one transaction. This sequence is the order of execution. The steps that matter for recursion are these:
- Before-save record-triggered flows run.
- Before triggers run.
- System and custom validation rules run, and the record is saved to the database without being committed.
- After triggers run.
- Assignment, auto-response, and workflow rules run.
- If a workflow rule has a field update, the record is updated again, and before and after triggers fire one more time.
- After-save flows and processes run. If they update records, those updates run their own full save sequence.
- Roll-up summary fields and sharing are recalculated.
- The transaction commits.
Salesforce publishes the full list in the Apex Developer Guide. The list above is a simplified version.
Where the second run comes from
There are two separate ways your trigger runs again.
A workflow field update. If a workflow rule changes a field on the record, Salesforce fires the before and after triggers one more time. Custom validation rules do not run again. Salesforce does this re-fire only once, so it cannot loop on its own.
DML that your own code or another automation starts. A flow, a process, or another trigger can update records, and each update starts a full save sequence of its own. If one of those updates touches a record your trigger handles, your trigger runs again inside the same transaction. This chain has no built-in limit short of Salesforce's trigger depth check, so it is the dangerous one.
Both cases happen in the same transaction. Governor limits are shared across all the runs, and so are static variables, which later chapters use for guards.
Seeing the double run
A debug line shows how often a trigger fires and with what context. This trigger logs each run:
trigger AccountTrigger on Account (before update, after update) {
System.debug('AccountTrigger ' + Trigger.operationType
+ ' for ' + Trigger.new.size() + ' record(s)');
}Suppose a workflow rule on Account has a field update. When you update one Account, the debug log shows four lines: BEFORE_UPDATE, AFTER_UPDATE, then BEFORE_UPDATE and AFTER_UPDATE again. The second pair is the re-fire.
Why this matters for your code
The order of execution explains two things you will use later in this tutorial.
- A before trigger runs ahead of the save, so changes you make to
Trigger.newthere need no extra DML. - An after trigger runs when the record is already saved. Updating a record there starts another save sequence, and that is how the trigger ends up calling itself.
The next chapter turns this into the concrete recursion patterns.
Exercise
A workflow rule on Account updates a field. You save one Account that has a before update trigger and an after update trigger. How many times does each trigger run, and which kind of validation does not run again?
Show answer
Each trigger runs twice. The workflow field update fires the before and after triggers one more time, and only once more. Custom validation rules do not run again on that second pass.