Sub topic 8
Where Flows Sit in the Order of Execution
This chapter assumes you can build a basic record-triggered flow and know that a transaction either commits fully or rolls back. It also assumes you know what Apex triggers and validation rules are. You don't need to write Apex, but one short example uses it to show the order.
Why the order matters
Salesforce runs a fixed sequence of steps every time a record is saved. Flows, Apex triggers, validation rules, duplicate rules, and other automation each run at a defined point in that sequence. The position decides three things:
- Which values your flow sees in
$Record. - Which other automation sees your flow's changes.
- Whether your changes are checked by validation rules afterward.
Most "my flow did nothing" and "something overwrote my value" bugs come from a wrong assumption about this order.
The simplified sequence
Salesforce documents a longer list than this chapter shows. The steps below are the ones that matter for flows, and the rest are grouped.
Two facts follow from the diagram.
- Before-save flows run before any before Apex trigger and before custom validation rules.
- After-save flows run after the record is saved, after after Apex triggers, and after workflow rules. Everything up to that point is still uncommitted.
What the before-save position gives you
A before-save flow runs while the record is still in memory. This has four consequences.
- No extra DML for the triggering record. An Assignment element on
{!$Record}changes the values that will be saved. No second save is needed. - No
Idon create. On an insert,{!$Record.Id}is empty because the record has not been saved. Don't use it to relate a child record here. That belongs in an after-save flow. - Your changes get validated. Validation rules and duplicate rules run after before-save flows. If your flow sets a bad value, a validation rule can reject the save. This is useful, because the flow can't put the record into a state your rules forbid.
- Apex before triggers see your changes. A before trigger reads the values the flow assigned.
You can see the fourth point with a trigger. This assumes the "Set Next Step" before-save flow from the earlier chapters is active.
trigger OpportunityOrderDemo on Opportunity (before insert, after insert) {
for (Opportunity opp : Trigger.new) {
if (Trigger.isBefore) {
System.debug('Before trigger sees NextStep: ' + opp.NextStep);
} else {
System.debug('After trigger sees NextStep: ' + opp.NextStep);
}
}
}Insert an Opportunity with Amount = 200000. Both debug lines show Review large deal, because the flow ran first. If you remove the Amount, both lines show null.
The reverse does not hold. If a before trigger changes NextStep, the before-save flow does not run again to react to that change. Apex that runs later has the last word on a field in the same save.
What the after-save position gives you
An after-save flow runs once the record is in the database but not yet committed.
{!$Record.Id}exists, so you can create children such as a Task withWhatId = {!$Record.Id}.$Recordreflects values set by before-save flows, before triggers, and workflow field updates that already ran.- The flow can create, update, or delete other records. It can also call actions such as sending email, invoking Apex, or calling a subflow.
- A change to another record is a separate DML operation. That operation starts its own order of execution for the other record.
Because the transaction is still open, an unhandled error in an after-save flow rolls back everything. That includes the original record, the before-save changes, and any Apex trigger work.
One transaction, shared limits
Before-save flows, after-save flows, and Apex triggers all run in the same transaction. They share one set of governor limits. The main synchronous limits are 100 SOQL queries, 150 DML statements, and 10 seconds of CPU time. They apply to the whole transaction, not to each automation.
| Resource used by the flow | Counts against the shared limit? |
|---|---|
| Get Records element | Yes, one SOQL query per execution of the element |
| Create, Update, or Delete Records on other records | Yes, DML statements |
| Assignment on `{!$Record}` in a before-save flow | No extra DML |
| Update Records on `{!$Record}` in an after-save flow | Yes, and it starts a nested save |
| Invoked Apex action | Yes, its own queries and DML count |
| Flow interview CPU time | Yes, it adds to the transaction CPU total |
When several records are saved in one operation, such as a data import of 200 rows, Salesforce bulkifies the flow. It batches the work for similar elements across the records in the chunk. You still need to keep queries and DML out of loops. The next chapter covers that.
Nested saves and re-triggering
An after-save flow that updates the triggering record with an Update Records element on {!$Record} causes a second save. That save runs the order of execution again for the record. Before-save flows, before triggers, validation rules, and after-save flows can all run a second time. The two saves share one transaction and one set of limits.
Workflow field updates are a special case. When a workflow rule changes a field, Salesforce fires the before and after update triggers once more, but it does not re-run validation or before-save flows for that step.
Two habits reduce surprises:
- Use a before-save flow to change fields on the triggering record. It avoids the second save.
- Write entry conditions that stop the flow from matching again after its own change. For example, use
ISCHANGEDtogether with the target value. The earlier entry-conditions chapter showed this pattern.
Asynchronous paths run after the commit
An after-save flow can add an asynchronous path. That path does not run in the order of execution above. It runs after the transaction commits, in its own transaction with its own limits.
This has two effects.
- Fresh limits. Heavy work, such as callouts or many queries, can move to the async path to protect the main transaction.
- No rollback link. If the async path fails, the original save is already committed and stays saved. If the main transaction fails, the async path never starts.
The values in $Record on an async path are the values at the time the path runs. They may have changed since the original save, so don't assume they match what the synchronous part saw.
Several flows and Apex on one object
Several before-save flows can exist on one object, and several after-save flows too. Their relative order is not guaranteed by default. If order matters, set the trigger order value on each flow (Flow Trigger Explorer shows this). Flows with a lower value run first, and the order applies within the same type. A before-save flow always runs before an after-save flow, and no trigger order setting changes that.
Don't make one flow depend on a side effect of another unless you have set the order. A better design merges dependent logic into one flow. A later chapter covers coexistence with Apex in more detail.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Value set by a before-save flow is later different | A before trigger, workflow field update, or after-save flow changed it | Find the later step that writes the field and remove or merge the conflict |
| Related child record has no parent link on insert | The flow is before-save, so `$Record.Id` is empty | Use an after-save flow |
| Flow ran twice for one user action | An after-save update to the same record started a nested save | Move the change to a before-save flow or add a guard condition |
| Whole save fails after the flow worked | A validation rule rejected the value the before-save flow set | Fix the assigned value or the rule |
| Async path reads old data | The record changed between the save and the path run | Re-query the record in the async path if freshness matters |
Exercises
Exercise
A before-save flow sets Opportunity.NextStep. A before-insert Apex trigger on Opportunity then sets NextStep to a different value. Which value is saved, and does the flow run again to react?
Show answer
The trigger's value is saved, because before triggers run after before-save flows and the later write wins. The flow does not run again. A before trigger change on the same record does not re-run before-save flows.
Exercise
You need to create a Task related to a new Opportunity when it is inserted as Closed Won. Why can't you do this in a before-save flow, and which flow type do you use instead?
Show answer
A before-save flow runs before the Opportunity is saved, so {!$Record.Id} is empty on insert and the Task can't be linked to it. Before-save flows also can't create records on other objects. Use an after-save flow, where {!$Record.Id} exists, and set WhatId to it.
Exercise
Where do you put work that needs fresh governor limits and should not hold up the user's save, and what happens to the original save if that work fails?
Show answer
Put it in an asynchronous path of an after-save flow. The path runs after the commit, in its own transaction with its own limits. If it fails, the original save stays committed.