Sub topic 7
One Transaction, Many Automations: Order of Execution
This chapter assumes you can read a simple before-save or after-save record-triggered flow and a basic Apex trigger. It also assumes you know the synchronous governor limits from the earlier chapter. Here you will learn the sequence Salesforce follows when one record save fires several automations, and how to predict what each automation sees.
One save, one transaction
A transaction is a set of database operations that succeed or fail together. When a user saves a record, Salesforce runs the validation, triggers, flows, and other automation for that save inside a single transaction. If any step throws an unhandled error, the whole transaction rolls back. This includes the record save itself and every change that other automations made.
Flow and Apex share this transaction. A record-triggered flow, an Apex trigger, and a validation rule on the same object are all steps in one sequence. They draw from the same pool of synchronous governor limits, such as 100 SOQL queries and 150 DML statements. A flow that runs six queries leaves fewer for your trigger.
The sequence, simplified
Salesforce documents a long, exact order of execution. This version keeps the steps that matter when you mix Flow and Apex.
Three points about this sequence matter most:
- Before-save flows run before before triggers. Before triggers run before validation rules.
- The database write at step F is not a commit. Everything after it can still roll back.
- After-save flows run after after triggers.
The table summarizes where each tool lands.
| Step | Automation | Can change the triggering record without extra DML | Sees changes made by earlier steps |
|---|---|---|---|
| Before-save | Record-triggered flow | Yes, by assigning to the record | Original input values |
| Before | Apex before trigger | Yes, by editing `Trigger.new` | Before-save flow changes |
| After | Apex after trigger | No, `Trigger.new` is read-only | All earlier changes, plus the record Id |
| After-save | Record-triggered flow | No, needs an Update Records element | Trigger and rule changes |
What each step can see
The order tells you what data each automation receives. Suppose a before-save flow sets Priority__c to High when Amount is above 100,000. A before trigger on the same object then reads Trigger.new and finds Priority__c already set to High. The trigger never sees the user's original value. A before trigger that overwrites the field replaces the flow's value, and validation rules then evaluate the trigger's value.
Validation rules run after before-save automation. A before-save flow or before trigger can therefore fix a bad value before the rule checks it. That also means a validation rule can block a save because of a value an automation produced. The error message may confuse users who never typed that value.
After-save automation can see the record Id on insert, which before-save automation cannot. Use after-save automation to create related records, because those need the parent Id. Use before-save automation to set fields on the triggering record, because it needs no additional DML statement and costs the least.
Ordering inside a single step
The sequence orders steps, but it does not order automations that sit in the same step.
- Several Apex triggers on one object and event run in no guaranteed order. Do not write code that depends on one running first.
- Several before-save flows on one object run in the order you configure. In Flow Trigger Explorer, in Setup, you can set a trigger order value for each flow on an object. Flows with a lower value run first. Flows with no value run after those with one, in no guaranteed order.
- Process Builder processes and workflow rules are legacy tools, and Salesforce ended support for them on December 31, 2025. Older orgs may still contain them. Their relative order is not guaranteed either.
A common design rule follows from this: keep one trigger per object and event, and have it call a handler class that runs the logic in a fixed order. For flows, keep one before-save and one after-save flow per object where you can, with decisions inside each flow.
When a save fires the whole cycle again
An update made by an automation is a new save. If an after trigger or after-save flow updates a record, Salesforce runs that record's order of execution again, inside the same transaction. The new run fires before and after triggers, flows, and validation rules for the updated record.
This is the most common source of surprising behavior in mixed automation:
- A flow updates the Opportunity. That update fires the Opportunity trigger. The trigger updates the same Opportunity. That update fires the flow again.
- Each pass consumes SOQL, DML, and CPU time from the shared limits.
- Without a stop condition, the loop continues until a limit or the trigger depth limit throws an error and rolls back the transaction.
Flows reduce this risk with entry conditions. Set the flow to run only when a field changed, using the option to run only when the record is updated to meet the condition requirements. Apex handles it by comparing old and new values and, when needed, a static variable. A static variable keeps its value for the whole transaction, so it can remember which records the code already processed.
trigger OpportunityTrigger on Opportunity (after update) {
OpportunityStageHandler.afterUpdate(Trigger.new, Trigger.oldMap);
}public with sharing class OpportunityStageHandler {
// Lives for the whole transaction, including re-entry from other automations
private static Set<Id> processedIds = new Set<Id>();
public static void afterUpdate(List<Opportunity> newList, Map<Id, Opportunity> oldMap) {
List<Task> tasks = new List<Task>();
for (Opportunity opp : newList) {
Opportunity oldOpp = oldMap.get(opp.Id);
Boolean justClosed = opp.StageName == 'Closed Won'
&& oldOpp.StageName != 'Closed Won';
if (justClosed && !processedIds.contains(opp.Id)) {
processedIds.add(opp.Id);
tasks.add(new Task(
Subject = 'Send welcome kit',
WhatId = opp.Id,
ActivityDate = Date.today().addDays(3)
));
}
}
insert tasks;
}
}The handler checks the stage change first, so the guard only matters when another automation re-saves the same record in the same transaction. Without the guard, an automation that touches the Opportunity again could cause a second task. In this example the oldOpp.StageName != 'Closed Won' check already stops most repeats, because the re-saved record's old stage is also Closed Won. The guard covers cases where an automation changes the stage away and back.
Rollback, errors, and what the user sees
Because everything in one save shares a transaction, an unhandled error in the last step undoes the first. An after-save flow that hits a limit rolls back a before trigger's field change, the record itself, and any records the trigger created.
You can contain failures in a few ways:
- In Flow, add a fault path to elements that can fail, such as Create Records and Update Records, and decide whether to stop or continue.
- In Apex, wrap risky work in
try/catch, or useDatabase.insert(records, false)to allow partial success and inspect each result. - Use
addError()on a trigger record to block that record with a message, which is the Apex equivalent of a validation rule.
Catching an error does not undo changes made earlier in the transaction. If a flow already updated a record and your Apex then catches an exception, the flow's update stays. Use a Savepoint and Database.rollback() if you need to undo part of the work yourself.
Post-commit work behaves differently. Emails, asynchronous paths in flows, and queued Apex jobs run only after the transaction commits. An asynchronous job gets its own transaction with its own limits, so a failure there does not roll back the original save.
Finding out what actually ran
Do not guess the order in a real org. Use these tools:
- Open Flow Trigger Explorer in Setup to see every record-triggered flow for an object, grouped by when it runs, and to set trigger order.
- Turn on a debug log for the user making the save. The log lists each automation in the order it ran, with limit usage at each step.
- Read the cumulative limit totals at the end of the log. They show the combined cost of the whole chain, which no single flow or class shows.
- Check the Salesforce documentation on the order of execution for your API version. Details change between releases.
Common errors
- Putting field defaults in an after-save flow and then paying for an extra update. A before-save flow does the same job with no DML.
- Expecting a before trigger to see the user's original value after a before-save flow changed it.
- Letting a trigger and a flow update each other's fields, producing a loop that ends in a limit error.
- Relying on trigger order without setting it, then losing the behavior after a deployment.
- Blaming a validation rule for a failure when an earlier automation set the bad value.
- Testing a flow and a trigger on one record while production loads 200 records at a time. Combined limit use grows with batch size, so test the whole chain in bulk.
Exercise
An Account has a before-save flow that sets Region__c from the billing country, one Apex before-update trigger that reads Region__c, and one after-save flow that creates a Task. A user changes the billing country. Which automation sees the new Region__c value, and why? What happens if the Task creation fails with an unhandled error?
Show answer
The before trigger sees the new Region__c value, because before-save flows run before before triggers and the trigger reads Trigger.new after the flow changed it. The after-save flow sees it too, since it runs later. If Task creation fails with an unhandled error, the whole transaction rolls back. The billing country change, the Region__c value, and the Task are all undone, and the user sees the error.
Exercise
An after-save flow updates an Opportunity's Next_Step__c. An Apex after-update trigger on Opportunity also updates Next_Step__c on the same record. Describe the risk, and name two ways to reduce it.
Show answer
Each update is a new save that runs the order of execution again, so the flow and the trigger can re-fire each other in a loop. The loop uses shared SOQL, DML, and CPU limits and ends in a limit error that rolls back the transaction. To reduce the risk, give the flow an entry condition that runs only when a relevant field changed, and have the trigger compare old and new values before it updates. You could also move the logic into a single automation. A static Set of processed Ids is a further option for actions that must run once.