Sub topic 7
Choosing between before-save and after-save
This chapter assumes you can build a record-triggered flow, read $Record and $Record__Prior, and write entry conditions. It covers how to pick the optimization setting for a requirement.
The default rule
Use before-save when the only job is to change fields on the record that triggered the flow. Use after-save for everything else. Before-save is faster and uses fewer resources, so choose it whenever it can do the job.
| Need | Before-save (Fast Field Updates) | After-save (Actions and Related Records) |
|---|---|---|
| Set fields on the triggering record | Yes, with an Assignment element | Possible, but needs an Update Records element and extra DML |
| Create or update other records | No | Yes |
| Use `{!$Record.Id}` on a new record | No, the Id does not exist yet | Yes |
| Send email, post to Chatter, call Apex or a subflow with actions | No | Yes |
| Use asynchronous or scheduled paths | No | Yes |
Why before-save is cheaper
A before-save flow runs before the record is written to the database. When you assign a value to {!$Record.NextStep}, the new value is saved along with the rest of the record. No second save happens, so no extra DML and no second pass through triggers, validation rules, or other automation.
An after-save flow runs after the record is saved. $Record is read-only at that point. To change the same record you must add an Update Records element. That element performs a separate DML operation, which counts against limits and re-runs the record's save logic.
What forces you to after-save
Choose after-save when any of these is true:
- The flow must create, update, or delete a different record, such as a follow-up Task.
- The flow needs the new record's Id, for example to set
WhatIdon a related record. - The flow runs an action: an email alert, an invocable Apex action, a subflow that does DML, or a callout.
- The flow needs an asynchronous or scheduled path.
The after-save example from the plan shows the Id case:
Flow: Opportunity - Create Follow-up Task (after-save)
Object: Opportunity
Trigger: A record is created or updated
Optimize for: Actions and Related Records
Entry condition: {!$Record.StageName} = "Closed Won"
Run: Only when a record is updated to meet the condition requirements
Create Records element (object: Task)
Subject: Send welcome package
WhatId: {!$Record.Id}
OwnerId: {!$Record.OwnerId}A before-save flow could not do this. It cannot create a Task, and {!$Record.Id} is empty during a create.
A decision path
Common mistakes
- Choosing after-save out of habit for a simple field update. The flow works, but it costs an extra DML statement and slows saves, and the cost grows with data volume.
- Splitting one requirement badly. If a requirement sets a field and also creates a Task, use two flows (one of each type) rather than forcing everything into after-save. The before-save flow handles the field cheaply.
- Updating the triggering record from an after-save flow without a tight entry condition. The extra save can re-trigger the flow. The recursion chapter covers this.
Verifying the choice
A before-save flow's effect is visible immediately after the insert, with no extra query for the Id. The Apex test from the plan checks this:
@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);
}
}Exercise
A flow must set Priority_c to "High" on a new Case when Amount_c is above 5000, and it must also email the account owner. Which optimization or optimizations do you use, and why?
Show answer
Use two flows. A before-save flow sets Priority__c with an Assignment element and needs no extra DML. An after-save flow sends the email, because before-save flows cannot run actions such as email alerts.
Exercise
An after-save flow creates a Task with WhatId set to the new Opportunity. Why can't this be a before-save flow?
Show answer
A before-save flow cannot create other records, and the Opportunity Id does not exist yet during a create, so there is nothing to put in WhatId.