Sub topic 16
Triggers in the order of execution
This chapter assumes you know the seven trigger events, the difference between before and after triggers, and how static variables guard against recursion. You don't need to memorize the full order of execution. You do need to know where triggers sit in it, because that position explains several behaviors you have already seen.
The save sequence around your trigger
When a record is saved, Salesforce runs a fixed sequence of steps. Your triggers are two steps in that sequence. The simplified order below keeps only the parts that matter for trigger work.
What the position tells you
Each placement in the sequence has a practical consequence.
| Position | Consequence |
|---|---|
| Before triggers run before custom validation rules | A before trigger can set a field so that a validation rule passes. |
| Before triggers run before the database write | Changes to `Trigger.new` are saved with no DML statement. |
| After triggers run after the write but before the commit | Record Ids exist, but `addError` or an unhandled exception still rolls everything back. |
| Workflow and after-save flows run after after triggers | They can change the record again, and your triggers may see that change. |
The workflow field update re-fire
If a workflow rule field update changes the record, Salesforce runs the before update and after update triggers one more time. Custom validation rules do not run again. This second run can surprise you, because Trigger.new now holds the values the workflow set.
You can observe the re-fire with a trigger that logs each run:
trigger AccountOrderDemo on Account (before update, after update) {
System.debug(Trigger.operationType + ' with ' + Trigger.new.size() + ' record(s)');
}If a workflow field update applies to the saved account, the debug log shows BEFORE_UPDATE and AFTER_UPDATE twice. A static guard like TriggerGuard from the recursion chapter would skip the second run entirely. That is only safe if your logic does not need to react to the workflow's changes. Guard on record Ids or on specific field changes if it does.
Choosing where logic belongs
Use the sequence to decide which event fits a requirement:
- Use a before trigger to adjust the record's own fields, or to fix values before validation rules check them.
- Use
addErrorin a before trigger to reject a record early, before the database write. - Use an after trigger when you need the record Id, for example to create related records.
- Treat anything in an after trigger as provisional until the commit. Emails and asynchronous jobs that you queue only take effect once the transaction commits.
The exact order of execution changes between releases. Check the current "Triggers and Order of Execution" page in the Apex Developer Guide before you rely on a detail not covered here.
Exercise
A validation rule requires Opportunity.Description to be non-blank on update. A before update trigger sets Description when it is blank. Will saving a record with a blank Description fail the validation rule? Explain using the order of execution.
Show answer
No, the save succeeds. Before triggers run before custom validation rules, so the trigger fills in Description first. When the validation rule runs, the field already has a value. An after update trigger could not do this, because after triggers run after validation has already passed.
Exercise
A workflow field update changes an Account after your after update trigger runs. How many times does the trigger run, and what does a static Boolean guard do on the second run?
Show answer
The before update and after update triggers run twice, once for the original save and once more after the workflow field update. The static Boolean is already true on the second run, so the guard returns early and skips the trigger body. This is only safe if you do not need to react to the workflow's changes.