Sub topic 8
Where Flows Sit in the Order of Execution
This chapter assumes you can build a basic record-triggered flow, know what a before-save and an after-save flow do, and understand that a save happens inside a database transaction. Here you learn where each flow type runs during a save, and why that placement matters.
Why the order matters
When a user saves a record, Salesforce does not run all automation at once. It runs a fixed sequence of steps. Your flow runs at one specific step. That step decides which values the flow can see, which later steps can overwrite its changes, and which limits it shares with other code.
The relevant steps
Salesforce documents a longer sequence. These are the steps that matter for flows, in order:
- The system loads the record and runs system validation, such as required fields and field formats.
- Before-save record-triggered flows run.
- Before Apex triggers run.
- Most system validation runs again, along with custom validation rules.
- The record is saved to the database but not yet committed.
- After Apex triggers run.
- Assignment rules, auto-response rules, and workflow rules run.
- After-save record-triggered flows run.
- Roll-up summary fields and other cross-object updates are calculated.
- The transaction commits.
- Post-commit logic runs, such as emails and asynchronous paths.
What this placement means
Before-save flows run ahead of custom validation rules. A value your flow sets is therefore checked by those rules. If the flow sets a value that breaks a rule, the save fails.
Before-save flows also run before the record exists in the database. For a new record, {!$Record.Id} is empty. Changes to the triggering record cost no extra save, because the platform writes them in the normal step 5.
After-save flows run once the record has been written. The record has an Id, so you can relate child records to it. Changes you make to the triggering record in an after-save flow need an Update Records element, and that causes a second save of the same record. A second save can run record-triggered automation again, which is the cause of the recursion problems covered in a later chapter.
Shared limits in one transaction
Everything from step 1 to step 10 runs in the same transaction. Flows, Apex triggers, and workflow share the same governor limits, such as 150 DML statements and 100 SOQL queries for synchronous work. An after-save flow that performs DML uses the same budget as your triggers. Asynchronous paths run later, in their own transaction, with their own limits.
A common error
Suppose an Apex before trigger and a before-save flow both change NextStep on the same Opportunity. The flow runs first, then the trigger. The trigger's value wins, and the flow's value is lost without any error.
The same reasoning applies to after-save flows. If an after-save flow updates the triggering record, the update starts a second pass through the sequence. Before-save flows and triggers run again for that second save.
Choosing based on position
- Use before-save when you only change fields on the triggering record and want validation rules to check the result.
- Use after-save when you need the record Id, related records, or actions such as sending an email or calling a subflow.
- Expect that cross-object effects of an after-save flow, such as roll-up recalculation, happen after your flow finishes.
Exercise
A before-save flow sets Opportunity NextStep to "Review large deal". An Apex before trigger on the same object sets NextStep to "Manager approval". Which value is saved, and why?
Show answer
"Manager approval". Before-save flows run before Apex before triggers, so the trigger runs later and overwrites the flow's value. Nothing reports an error.
Exercise
You need to create a Task related to a newly created Opportunity. Why can't a before-save flow do this, given where it runs in the order of execution?
Show answer
A before-save flow runs before the record is written to the database, so a new record has no Id yet. The Task needs the Opportunity Id for WhatId. An after-save flow runs after the write and has the Id.