Sub topic 11
Workflow field updates fire triggers again
This chapter assumes you can write a basic Apex trigger, know what Trigger.new and Trigger.old contain, and have seen a workflow rule with a field update. Workflow rules are a legacy tool, but many orgs still have them. They are the one automation that makes the save run through the trigger steps twice.
What happens
A workflow rule runs after the after triggers. If the rule has a field update that actually changes a value, Salesforce saves that change and then runs these steps again:
- System validation.
- Before update triggers.
- After update triggers.
This happens one more time only, per workflow field update pass. The re-fire is always an update. If the original operation was an insert, the second pass fires before update and after update, not insert triggers.
What does not run again
Only part of the sequence repeats. On the re-fire:
- System validation and the update triggers run again.
- Custom validation rules, duplicate rules, and escalation rules do not run again.
- Flows do not run again as a result of the workflow field update.
A record that passed your custom validation rules can therefore be changed by a workflow update without those rules checking the new value. Don't count on validation rules to protect a field that workflow can write.
Conditions that trigger the re-fire
- The field update must change the stored value. If the workflow writes the value the field already holds, nothing is saved again and no triggers re-fire.
- The re-fire is a property of workflow field updates. Other post-save steps have different effects, covered in other chapters.
- If the field update has "Re-evaluate Workflow Rules after Field Change" selected, workflow rules are evaluated again against the changed record. This can lead to further field updates, so use it deliberately.
Seeing it in a trigger
This trigger logs each pass. When a workflow field update changes an Account, you will see two lines for one save.
trigger AccountPassCounter on Account (before update, after update) {
for (Account a : Trigger.new) {
System.debug(Trigger.operationType + ' for ' + a.Id
+ ' Rating=' + a.Rating
+ ' Description=' + a.Description);
}
}On the first pass the values are the ones your save supplied. On the second pass Trigger.new includes the value written by the workflow field update. The before trigger, which can assign fields without DML, also sees the record a second time. Any assignment it makes is applied again unless you guard against it.
Why it matters
Three problems come from the second pass:
- Duplicate side effects. An after update trigger that sends an email or creates a related record does the work twice for one save.
- Doubled counters. Logic such as
a.Counter__c = a.Counter__c + 1in a before update trigger runs on both passes. - Extra limit use. The second pass runs inside the same transaction, so its queries and DML count against the same governor limits.
Guarding with a static set
A static variable lives for the whole transaction, so it survives the re-fire. Record the Ids you have processed and skip them on the second pass.
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
}A Boolean flag such as hasRun is a common alternative, but it is the weaker choice. It stops the whole trigger on the second pass for every record in the batch. In a transaction that saves records in chunks of 200, or that updates other records later, a Boolean can skip records that were never processed. The Id set skips only records you have handled.
Guarding by what changed
An Id guard has a cost. If the workflow update changes a field you care about, the guard hides that change from your trigger. A narrower approach is to act only when the field you depend on changed. This version creates a task when Rating becomes Hot, and it does so once even if the trigger runs twice, because on the second pass Rating has not changed again.
trigger AccountHotRating on Account (after update) {
List<Task> tasks = new List<Task>();
for (Account a : Trigger.new) {
Account oldA = Trigger.oldMap.get(a.Id);
if (a.Rating == 'Hot' && oldA.Rating != 'Hot') {
tasks.add(new Task(Subject = 'Call hot account', WhatId = a.Id));
}
}
if (!tasks.isEmpty()) {
insert tasks;
}
}Confirm in a debug log what Trigger.oldMap holds on the second pass in your org, because this depends on how your workflow updates are configured. Test with the real rule before you rely on it.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Assuming a save runs triggers once | Duplicate emails, tasks, or counters | Log `Trigger.operationType` and check for a second pass |
| Expecting insert triggers on the re-fire | Logic placed in `before insert` never sees the workflow value | Handle the value in update triggers |
| Relying on validation rules after workflow | The workflow value skips custom validation | Validate in a trigger or restrict the field update |
| Using a static Boolean for the guard | Skips records that were never processed | Track processed Ids in a `Set<Id>` |
Design note
Each new field update adds a possible second trigger pass. Where you can, move that logic to a before-save flow or a before trigger, which set the value in the first pass without a re-fire.
Exercise
A user inserts an Account. A workflow rule with a field update changes Rating from blank to Hot. Which trigger events fire, in order, for this one save, and which validation steps repeat on the second pass?
Show answer
On the first pass, before insert and then after insert fire. After the workflow field update changes the value, before update and then after update fire once. System validation repeats on the second pass. Custom validation rules, duplicate rules, and escalation rules do not run again.
Exercise
An after update trigger creates a Task for every Account in Trigger.new, and users report two Tasks per save when a workflow field update is active. Give two ways to fix it.
Show answer
Track processed Ids in a static Set<Id> and skip records already in it, so the second pass does nothing for them. Or create the Task only when the relevant field changed, by comparing Trigger.new with Trigger.oldMap, so the second pass does not meet the condition again.