Sub topic 5
Before-save flows: updating the triggering record
This chapter assumes you know the Salesforce data model, the basic Flow Builder elements (Assignment, Decision, Get Records), formulas, and what a database transaction is. It also assumes you have read the earlier chapters on trigger configuration, entry conditions, and $Record.
What a before-save flow does
A before-save flow runs after a record is created or updated but before it is written to the database. In Flow Builder you pick it by setting Optimize the Flow for to Fast Field Updates.
The flow edits the in-memory copy of the record that is about to be saved. When the flow finishes, the platform saves the record with your changes included. This is why no Update Records element and no extra DML (a database write) is needed. One save writes both the user's changes and the flow's changes.
Building one
The example below sets a reminder on large Opportunities.
Flow: Opportunity - Set Next Step (before-save)
Object: Opportunity
Trigger: A record is created or updated
Optimize for: Fast Field Updates
Entry condition: {!$Record.Amount} > 100000
Run: Only when a record is updated to meet the condition requirements
Assignment element
Variable: {!$Record.NextStep}
Operator: Equals
Value: Review large dealThree details matter here:
- The Assignment element writes directly to
{!$Record.NextStep}. You are changing the triggering record itself, not a copy held in a separate variable. - The entry condition keeps the flow from running on every Opportunity save.
- The run setting "Only when a record is updated to meet the condition requirements" fires the flow when the record starts to match, not on every later edit. A user's later edits to an already-large deal will not overwrite
NextStepagain.
What a before-save flow cannot do
The limits follow from the timing. The record has not been saved yet, so:
{!$Record.Id}is empty on a create. You cannot use it to link child records.- You cannot create, update, or delete other records. Create Records, Update Records on other objects, and Delete Records are not available.
- You cannot run actions such as sending email or posting to Chatter, and you cannot use asynchronous paths.
- You can read other data with Get Records, and you can use Decisions, Loops, formulas, and Assignments.
Common errors
- Adding an Update Records element for the triggering record. In a before-save flow, use an Assignment to
$Record. In an after-save flow, the same pattern causes a second save and can re-trigger automation. - Assuming the Id exists. Formulas that rely on
$Record.Idreturn nothing on create. - Overwriting user input. If your Assignment writes a field the user also edited, your value wins. Check with
ISCHANGEDor$Record__Priorwhen you want to respect the user's edit. - Writing the entry condition too loosely. A condition such as
Amount > 100000with the default run setting will run on every save of a large deal. Use the "only when a record is updated to meet the condition requirements" setting, or a stricter formula.
Verifying with an Apex test
Because the change happens in the same save, a test can insert the record and read it back. This test assumes the flow above is active in the org.
@IsTest
private class OpportunityFlowTest {
@IsTest
static void beforeSaveFlowSetsNextStep() {
Opportunity opp = new Opportunity(
Name = 'Large deal',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
Amount = 200000
);
insert opp;
Opportunity saved = [SELECT NextStep FROM Opportunity WHERE Id = :opp.Id];
Assert.areEqual('Review large deal', saved.NextStep);
}
}The query reads the stored value, so the assertion proves the flow's change was persisted, not just held in memory.
Exercise
A requirement says: when an Opportunity's Amount is set above 100000, set NextStep, and also create a Task for the owner. Can one before-save flow do both? Explain.
Show answer
No. The NextStep change fits a before-save flow, because it edits the triggering record through an Assignment on $Record. Creating a Task needs Create Records and the record's Id, which does not exist before save on a create. Use a before-save flow for the field update and an after-save flow for the Task, or do both in one after-save flow if you accept the extra save.