Sub topic 7
One Transaction, Many Automations: Order of Execution
This chapter assumes you know how to build a record-triggered flow, can read a basic Apex trigger, and have read the chapter on governor limits. It explains what happens when one save fires several automations at once.
One save, one transaction
A transaction is a single unit of work that either commits completely or rolls back completely. When a user saves a record, validation rules, before-save flows, Apex triggers, and after-save flows all run inside the same transaction. They share one set of governor limits, such as 100 SOQL queries and 150 DML statements for synchronous Apex. If any step throws an unhandled error, the whole save is rolled back, including changes made by earlier steps.
Flow and Apex do not run in the order you built them or in alphabetical order. Salesforce runs them in a fixed sequence called the order of execution.
The simplified sequence
Salesforce documents a longer list. This version keeps the steps that matter most when choosing between Flow and Apex.
Three points follow from this sequence:
- Before-save flows run before before triggers. A trigger sees the values a flow has already set.
- Validation rules run after both. A before-save flow or trigger can fix a value before a rule checks it.
- After-save flows run after after triggers. If a flow updates the same record, that update starts the process again.
Why the order matters
When two automations touch the same field, the one that runs later wins. Suppose a before-save flow sets Priority__c to High, and a before trigger on the same object sets it to Normal. The saved value is Normal. Nothing warns you about the overwrite.
A second problem is re-entry. If an after-save flow or trigger updates the record that started the save, the save runs again. Salesforce repeats before and after triggers and flows for that update. Each repeat uses more of the shared limits, and a loop of updates ends in a limit error.
Apex can guard against re-entry with a static variable. A static variable keeps its value for the whole transaction, so it remembers what has already been handled.
public class RecursionGuard {
private static Set<Id> processed = new Set<Id>();
// Returns true the first time an Id is seen in this transaction.
public static Boolean firstRun(Id recordId) {
return processed.add(recordId);
}
}trigger OpportunityTrigger on Opportunity (after update) {
List<Task> tasks = new List<Task>();
for (Opportunity o : Trigger.new) {
Boolean justWon = o.StageName == 'Closed Won'
&& Trigger.oldMap.get(o.Id).StageName != 'Closed Won';
if (justWon && RecursionGuard.firstRun(o.Id)) {
tasks.add(new Task(Subject = 'Send welcome kit', WhatId = o.Id));
}
}
insert tasks;
}Set.add returns false when the value is already in the set. A second pass through the trigger in the same transaction therefore skips the opportunity. The trigger also compares old and new stage values, which is the best first defence. Flow has no static variables, so a flow guards against re-entry with entry conditions such as "only when the field is changed".
Practical rules
- Make an after-save flow or trigger update only the fields that need it. Updates to the triggering record itself restart the sequence, so prefer before-save flows or before triggers for same-record changes. They need no extra DML.
- Add entry conditions to flows, and compare old and new values in triggers, so automations run only when a relevant field changes.
- Do not rely on the order of several flows on one object and event. Flow Trigger Explorer in Setup lets you set an order, but combining logic into fewer automations is safer.
- Treat the shared limits as one budget. A flow that runs 40 queries leaves less for your trigger.
- Debug by reading a debug log, which shows each automation as it starts and ends, in the order it actually ran.
Exercise
A before-save flow sets Discount_c to 10 on Opportunity. An Apex before-update trigger on Opportunity sets Discount_c to 5. What is the saved value, and why?
Show answer
The saved value is 5. Before-save flows run before before triggers, so the trigger runs later and overwrites the flow's value. Fix it by moving the logic into one automation, or by giving each automation a distinct field.
Exercise
An after-save flow updates a field on the record that triggered it, and an Apex trigger on the same object does DML on every update. Why does this risk hitting governor limits, and what is the first control to add?
Show answer
Updating the triggering record restarts the save sequence, so the flow and the trigger run again, and each pass uses the same transaction's limits. Add entry conditions to the flow, and compare old and new values in the trigger, so each automation runs only when a relevant field actually changes.