Sub topic 4
Before-save flows run first
This chapter assumes you can read a basic Apex trigger and have built a simple record-triggered flow in Flow Builder. It also assumes you know the earlier steps: the original record is loaded, new values overwrite it, and system validation runs.
Where before-save flows sit
Before-save record-triggered flows run after system validation and before any before trigger. A flow can therefore change the record, and every later step sees the changed values. Those later steps include before triggers, custom validation rules, and the database save.
What a before-save flow does
When you create a record-triggered flow, you choose Fast Field Updates to get a before-save flow. The other choice, Actions and Related Records, gives an after-save flow, covered in a later chapter.
A before-save flow edits the triggering record through the $Record variable. You assign values to its fields. You don't add an Update Records element and you don't issue DML. The platform was already going to save this record, so your changes are included in that save.
This is the same idea as a before trigger that sets a.Description = '...' on Trigger.new. It is also why before-save flows are much faster than after-save flows. They add no extra save of the same record, and they don't re-run other automation.
What it can and cannot do
| Capability | Before-save flow |
|---|---|
| Change fields on the triggering record | Yes, by assigning to `$Record` |
| Read other records with Get Records | Yes |
| Use Decision, Assignment, Loop, and Formula | Yes |
| Create, update, or delete other records | No |
| Call actions, subflows, or make callouts | No |
| Use the new record's `Id` on insert | No, it is still null |
| Run again after its own field changes | No |
The Id limit is the same as in a before insert trigger. The record hasn't been written to the database yet, so no Id exists. If your logic must create a related record that points at the new one, it belongs in an after-save flow or an after trigger.
Entry conditions and when the flow runs
You choose which operations start the flow: create, update, or both. You can also add entry conditions that filter on field values. For updates, you can pick Only when a record is updated to meet the condition requirements. The flow then runs only when the record changes from not matching to matching, not on every later edit.
Compare these two ways to run an update flow:
- Run every time a matching record is updated. The logic repeats on each save while the condition stays true.
- Run only when updated to meet the conditions. The logic runs once for each transition into the matching state.
Pick the second option when the flow does something that should happen once, such as stamping a date. Otherwise the stamp would be overwritten on every save.
Seeing the flow's changes in a trigger
Because the flow runs first, a before trigger on the same object reads the flow's values in Trigger.new. Suppose a before-save flow on Account sets Rating to Hot when AnnualRevenue is over 1,000,000. This trigger confirms what it sees:
trigger AccountSeesFlow on Account (before insert, before update) {
for (Account a : Trigger.new) {
System.debug('Rating seen by trigger: ' + a.Rating
+ ', revenue: ' + a.AnnualRevenue);
}
}Insert an Account with AnnualRevenue = 2000000 and no Rating. The debug line shows Hot, even though the user never entered it. The flow changed the value earlier in the same transaction.
The reverse does not hold. If your trigger changes AnnualRevenue, the flow has already run and does not see the change. Order matters here. Don't design a flow that needs a value a before trigger will compute.
Gotchas
- Validation timing. Custom validation rules run after before triggers, so they see the flow's values. A flow can therefore fix a bad value before a rule would have rejected it. It can also introduce a value that makes a rule fail.
- Required fields. System validation of required fields and formats runs before the flow. Clearing a required field inside the flow can still be caught by the validation that runs again after before triggers. Don't rely on the first check.
- Workflow field updates. When a workflow field update changes the record, before and after update triggers run once more. Flows and custom validation rules do not run again. A before-save flow's logic therefore applies to the original save only.
- Bulk saves. Flows run on batches of records, not one at a time. Get Records inside a Loop element counts against the SOQL query limit, which is shared with triggers in the same transaction. Fetch once before the loop, or use a collection.
- Ordering. If you have several before-save flows on one object, set their trigger order in Flow Trigger Explorer. Don't assume names or creation dates decide it. A later chapter covers this.
Choosing between a before-save flow and a before trigger
Use a before-save flow when the job is simple field logic that an admin should be able to maintain. Typical jobs are defaulting values, normalizing text, and calculating a field from other fields on the same record. Use a before trigger when you need Apex features such as complex collections, shared utility classes, or unit-tested logic with fine control.
Exercise
An Account insert fires a before-save flow that sets Rating to Hot, and a before insert trigger that reads Rating. A second before-save flow tries to create a Task whose WhatId is the new Account's Id. What does the trigger see for Rating, and why can't the flow create the Task?
Show answer
The trigger sees Hot, because before-save flows run before before triggers and their field changes are already on the record. The flow can't create the Task for two reasons. Before-save flows can't create other records, and the Account has no Id yet because it hasn't been saved. The Task belongs in an after-save flow or an after insert trigger.