Sub topic 13
Where flows run in the order of execution
This chapter assumes you can read a Workflow Rule or a Process Builder process and that you know what a record-triggered flow is. It also assumes you have met validation rules and Apex triggers. Moving automation to Flow changes when your logic runs relative to those other features, and that is a common source of "it worked before" bugs after a migration.
Why the position matters
Every save of a record, whether from the UI, an API call, or Apex DML, goes through a fixed sequence of steps called the order of execution. Each automation tool runs at a set point in that sequence. Moving logic from one tool to another changes its position, even when the logic itself is identical.
Three consequences matter most for migration:
- A before-save flow runs before Apex before triggers and before validation rules. A Workflow Rule field update runs after both.
- A Process Builder process and an after-save flow both run after the record is written to the database (not yet committed). A process runs before after-save flows.
- Workflow Rules run earlier in the after-save phase than processes and flows.
The sequence as it applies to flows
The list below is simplified to the steps that matter when you migrate. Salesforce documents the full list under "Triggers and Order of Execution" in the Apex Developer Guide, and you should check it for your release.
- The system loads the original record, or initializes a new one for an insert, and applies the incoming field values.
- Before-save record-triggered flows run.
- Apex before triggers run.
- System validation and custom validation rules run, followed by duplicate rules.
- The record is saved to the database, but the transaction is not committed.
- Apex after triggers run.
- Assignment rules, auto-response rules, and Workflow Rules run.
- Escalation rules run.
- Processes from Process Builder, and any flows those processes launch, run.
- After-save record-triggered flows run.
- Entitlement rules run, then roll-up summary fields and cross-object updates are calculated, then criteria-based sharing is evaluated.
- The transaction is committed.
- Post-commit logic runs, including email sends and asynchronous work such as scheduled paths and Run Asynchronously paths.
What changes when you migrate
The table compares the old position of each tool with its position after migration.
| Logic you are migrating | Old position | New position | Practical effect |
|---|---|---|---|
| Workflow field update | After the database save and after triggers | Before-save flow, before triggers and validation | Validation rules and triggers now see the updated value |
| Process Builder field update on the same record | After the save, as a separate update | Before-save flow, in the same save | No second update and no re-fired triggers |
| Process Builder create or update of related records | After Workflow Rules | After-save flow | Runs later relative to other flows, same phase relative to triggers |
| Workflow email alert | After the save | After-save flow with an email action | Email still sends at the end, after commit |
| Time-dependent action or scheduled action | Queued after commit | Scheduled path | Runs in a separate transaction at the scheduled time |
Validation rules now see flow results
A Workflow Rule field update happened after validation, so a validation rule never evaluated the value the workflow set. A before-save flow sets the value first. A validation rule that was silently passing can now fail, and a rule that required the user to enter a value may now be satisfied by the flow instead.
Suppose a workflow set Region__c from the account's country, and a validation rule requires Region__c for Closed Won opportunities. Before the migration, the rule judged what the user entered. After the migration, it judges what the flow set. Review every validation rule on an object whose field updates you move.
Apex triggers see flow values and can overwrite them
A before trigger now runs after your before-save flow. If both write to the same field, the trigger wins. Before the migration, the workflow field update ran later and won. If you have a trigger and a workflow updating the same field, decide which should be authoritative and remove the other write.
The re-fire behavior of workflow field updates goes away
When a Workflow Rule field update changed a record, Salesforce fired the before and after update triggers one more time. A before-save flow changes the record before the first trigger pass, so no second pass happens. Code that depended on seeing the "second" invocation, for example by checking for the workflow-set value, needs review.
Chained automation re-enters the sequence
An after-save flow that updates the triggering record, or any related record, performs DML. That DML starts its own order of execution for the updated records. Before-save flows, triggers, and validation run again for those records, and other after-save flows can fire. This is how automation chains form, and why the recursion controls in the later chapter on bulk safety matter.
Order among flows in the same phase
Two before-save flows on the same object run at the same step, as do two after-save flows. Salesforce does not guarantee their relative order unless you set it. You can assign a trigger order value to each record-triggered flow, and Flow Trigger Explorer in Setup shows every flow on an object grouped by trigger type, ordered by that value. Flows with a lower value run first. Flows without a value run after those that have one, in an unspecified order.
Two points are easy to get wrong:
- The trigger order applies within a phase. It cannot move an after-save flow ahead of a before-save flow.
- A flow that relies on another flow's output in the same phase is fragile. Either set the order explicitly or merge the logic into one flow, which the consolidation chapter covers.
Checking the order in practice
Do not rely on memory when behavior is surprising. Use a sandbox and the debug log:
- Turn on a debug log for your user with workflow and Apex code logging at a detailed level.
- Save one record that meets the entry conditions.
- Search the log for the flow, trigger, validation, and workflow entries and read their sequence.
For flows, the log shows entries such as FLOW_START_INTERVIEWS_BEGIN, so you can see where each flow starts relative to CODE_UNIT_STARTED lines for triggers. Compare the sequence against the table above for each object you migrate.
Common errors
- Assuming flows run where workflows ran. A migrated field update moves earlier. Re-test validation rules and triggers on the object.
- Leaving the old and new automation both active. Both fire, and the order between the old tool and the new flow is fixed by the table, not by your intent. Deactivate the old one at cutover.
- Depending on undefined ordering. Two after-save flows that read each other's results will behave inconsistently until you set the trigger order or merge them.
- Expecting post-commit work inside the transaction. Scheduled paths and emails run after the commit. A failure there does not roll back the saved record.
Exercise
A Workflow Rule used to set Priority__c to "High" when an Opportunity amount exceeded 100000. A validation rule requires Priority__c to be filled for those opportunities. After you migrate the workflow to a before-save flow, saves that used to fail validation now succeed. Why, and what should you check?
Show answer
Workflow field updates ran after validation, so the validation rule saw only the user's value and failed when it was blank. A before-save flow runs before validation, so the flow sets Priority__c first and the rule passes. Check whether this new behavior is intended. Also review any Apex before trigger that writes Priority__c, because it now runs after the flow and can overwrite the value.
Exercise
You have two after-save flows on Case. Flow B reads a field that Flow A sets. After the migration, Flow B sometimes sees an empty value. What are two ways to fix this?
Show answer
Set the trigger order so Flow A has a lower value than Flow B, which makes it run first. Or merge both into one flow in which the step that sets the field comes before the step that reads it. Merging is more reliable because it does not depend on ordering settings.