Sub topic 10
Workflow field updates fire triggers again
This chapter assumes you know the order of execution up to the after triggers. It covers one exception to the usual top-to-bottom flow, which is a loop back to the before-update step.
What happens
A workflow rule can have a field update action. If that action changes a field value, Salesforce saves the new value and runs the update triggers again. Before update triggers run, then after update triggers run. The same happens when the original save was an insert. The second pass is always an update.
Three details matter:
- The re-fire happens only when the field update actually changes the value.
- It happens once. Salesforce does not loop on workflow updates alone.
- Custom validation rules, duplicate rules, and before-save flows do not run again. System validation does.
Why it causes bugs
Your trigger code runs twice in one transaction. If an after update trigger creates a Task each time, you get two Tasks. The same happens with counters, callouts, and emails.
On the second pass, Trigger.new holds the record with the workflow's new value. Trigger.old lets you see which field changed. Check that difference before you act.
Guarding the second pass
A static variable lives for the whole transaction. Use it to remember which records you have already handled.
public class TriggerGuard {
public static Set<Id> processed = new Set<Id>();
}trigger AccountAfterUpdate on Account (after update) {
List<Account> toProcess = new List<Account>();
for (Account a : Trigger.new) {
if (!TriggerGuard.processed.contains(a.Id)) {
TriggerGuard.processed.add(a.Id);
toProcess.add(a);
}
}
// Work only with toProcess here
}The trigger adds each Id to the set on its first pass. On the second pass the Ids are already in the set, so toProcess is empty.
Choosing an approach
| Approach | Use when |
|---|---|
| Static Id set | The work must happen once per record per transaction |
| Compare old and new values | You need to react only when a specific field changes |
| Move the field update into a before trigger or before-save flow | You control the logic and want to avoid the extra pass |
The last row is often the best fix. A before trigger changes values without DML and without re-firing anything.
Exercise
An Account is inserted. A workflow rule then updates a field on it, and the value changes. Which triggers run in this transaction, and which validation does not run again?
Show answer
Before insert and after insert run first. After the workflow field update, before update and after update run once. System validation runs again, but custom validation rules, duplicate rules, and before-save flows do not.