Sub topic 14
Order of execution: what runs when a record is saved
This chapter assumes you know how to build a simple record-triggered flow, what a validation rule and an Apex trigger are, and what a DML statement is. You do not need to write Apex. A few short examples appear, and you can read them as logic.
Why the order matters
When you click Save, Salesforce does not run "the automation" as one step. It runs a fixed sequence of steps in one transaction. Every tool on the object gets a turn at a defined point. If you know the sequence, you can answer questions like these:
- Why did my validation rule not see the value my flow just set?
- Why did my trigger run twice?
- Why does my after-save flow see a field value that differs from what the user typed?
The sequence also explains why some tools cannot change certain things. A before-save flow, for example, cannot send an email.
The sequence for a record save
Salesforce publishes the full order in its developer documentation, and it has many more steps than shown here. The list below covers the steps that matter for choosing a tool. Check the official "Triggers and Order of Execution" page for exact details, because the order changes between releases.
- Salesforce loads the existing record, or starts a new one for an insert, and overwrites it with the incoming field values.
- Before-save record-triggered flows run.
- Before Apex triggers run.
- System validation and custom validation rules run, then duplicate rules.
- The record is saved to the database, but it is not committed yet.
- After Apex triggers run.
- Assignment rules, auto-response rules, and escalation rules run.
- Legacy workflow rules run. If a workflow field update changes the record, the before and after update triggers fire one more time.
- Process Builder processes and the flows they launch run, followed by after-save record-triggered flows.
- Roll-up summary fields and cross-object formula updates recalculate on the parent record. The parent then goes through its own save sequence.
- Sharing rules are evaluated, and the transaction commits.
- Post-commit work runs: email sends, and queued asynchronous Apex such as Queueable jobs.
What the sequence tells you
Before-save logic runs before validation
Before-save flows and before triggers run before custom validation rules. A before-save flow can therefore set a field to a value that your validation rule then checks. This is one reason to prefer before-save for changing fields on the same record. The change costs no extra DML statement, and validation sees the final value.
After-save logic runs after validation. An after-save flow cannot fix a value that a validation rule has already rejected.
Before-save flows cannot reach other records
At the before stage the record has no ID on insert and is not in the database yet. Before-save flows can only change the record being saved. To create or update related records, send emails, or call actions, use an after-save flow or an after trigger.
After-save changes cause another save
If an after-save flow or trigger updates the same record, Salesforce saves it again. That second save can run your automation again, which is the usual cause of "my trigger ran twice." Updating a different record starts that record's own save sequence inside the same transaction.
All steps share one set of limits
Flows, triggers, workflow field updates, and process builders all run inside one transaction. They share one pool of governor limits, such as 100 SOQL queries and 150 DML statements for synchronous Apex. A flow that runs a query and a trigger that runs a query both count toward the same total. Asynchronous work, such as a Queueable job, gets its own limits because it runs in its own transaction after the commit.
Controlling order within one step
The sequence fixes the order between kinds of automation. Inside a single step, you have less control.
| Step | Can you control the order? |
|---|---|
| Several before-save flows on one object | Yes. Use the trigger order setting in Flow Trigger Explorer, from 1 to 2000. Flows without a value run after those with one, and ties are not guaranteed. |
| Several after-save flows on one object | Yes, the same way. |
| Several Apex triggers on one object | No. Salesforce does not guarantee the order. Use one trigger per object and call handler classes in a fixed order. |
| Workflow rules and Process Builder | No guaranteed order. Another reason to migrate them. |
If two automations must run in a specific order, put them in the same tool. Two steps in one flow, or two handler methods in one trigger, are easier to reason about than two separate automations that depend on timing.
Example: guarding against a repeat run
Suppose an after-update trigger updates the same account, or a workflow field update causes the trigger to fire again. A static variable lasts for the whole transaction, so it can record which records the trigger has already handled.
public class AccountTriggerGuard {
public static Set<Id> processedIds = new Set<Id>();
}trigger AccountAfterUpdate on Account (after update) {
List<Account> toUpdate = new List<Account>();
for (Account acc : Trigger.new) {
if (AccountTriggerGuard.processedIds.contains(acc.Id)) {
continue;
}
AccountTriggerGuard.processedIds.add(acc.Id);
// Trigger.new records are read-only in after triggers, so create a new instance.
toUpdate.add(new Account(Id = acc.Id, Description = 'Reviewed'));
}
if (!toUpdate.isEmpty()) {
update toUpdate;
}
}The update statement fires the trigger again. The guard makes the second run skip every account already in the set. Note two details. Trigger.new records are read-only in after triggers, so the code builds new Account instances. The code also uses one DML statement for the whole list, which keeps it bulk safe.
A blanket guard has a risk. If the second run legitimately needs to process a record again, because a later step changed it, the guard hides that run. Prefer a guard that checks whether the specific field actually changed, by comparing acc.Description against Trigger.oldMap.get(acc.Id).Description.
Common errors
- Assuming flows and triggers run in the order you built them. Creation date has no effect. Only the sequence above and the flow trigger order setting do.
- Expecting a validation rule to see after-save changes. Validation runs before after-save flows, so it checks the value as it was before them.
- Reading old values in an after trigger and expecting the final record. Workflow field updates and after-save flows can change the record after your trigger runs. Query the record again if you need the final state.
- Building update loops. Two automations that each update the other's trigger field can recurse until a limit error. Add entry conditions that check that a field changed.
- Putting callouts in synchronous steps. Callouts to external systems are not allowed in the middle of a transaction that has uncommitted DML. Use asynchronous Apex or an asynchronous path in a flow.
Exercise
An object has a before-save flow that sets Priority_c to "High" when Amount is over 100000, and a validation rule that blocks saving when Amount is over 100000 and Priorityc is not "High". A user saves a record with Amount 150000 and Priority_c blank. Does the save succeed, and why?
Show answer
The save succeeds. The before-save flow runs before custom validation rules, so it sets Priority__c to "High" first. When the validation rule runs, the condition is satisfied and the rule does not block the save. If the flow were after-save, the validation rule would run first and reject the record.
Exercise
A team has three Apex triggers on Opportunity, and a bug appears only when trigger B runs before trigger A. What does Salesforce guarantee about their order, and what is the usual fix?
Show answer
Salesforce does not guarantee the order of multiple triggers on one object. The usual fix is to keep a single trigger per object and call handler methods in an explicit order, so the sequence is defined in code.