Sub topic 5
Record-triggered flows: before-save versus after-save
This chapter assumes you know the Salesforce data model, can open Flow Builder from Setup, and can read simple Apex. A record-triggered flow starts when a record is created, updated, or deleted. When you build one, you choose between two timings, and that choice affects speed, capability, and the order in which automations run.
The two timings
A before-save flow (called "Fast Field Updates" in Flow Builder) runs before the record is written to the database. It can change fields on the triggering record only. It does not use a DML statement (a database write such as an insert or update), because the platform saves your changes along with the record.
An after-save flow (called "Actions and Related Records") runs after the record is written. The record now has an Id, and the flow can do more:
- Create or update related records.
- Send emails and post to Chatter.
- Call Apex actions, subflows, and external services.
- Update the triggering record, at the cost of an extra save.
Comparing them
| Question | Before-save | After-save |
|---|---|---|
| Can it change the triggering record? | Yes, with no extra DML | Yes, but it causes a second save |
| Can it touch other records? | No | Yes |
| Does `$Record.Id` exist on create? | No | Yes |
| Relative performance | Fastest | Slower |
Choosing between them
Default to before-save when the only job is to set fields on the record being saved. It is the declarative equivalent of this Apex before trigger:
trigger AccountTrigger on Account (before insert, before update) {
for (Account acc : Trigger.new) {
if (acc.AnnualRevenue != null && acc.AnnualRevenue >= 1000000) {
acc.Rating = 'Hot';
}
}
}In Flow Builder, you would configure the same rule like this:
- Create a record-triggered flow on Account, triggered when a record is created or updated.
- Set the entry condition to
AnnualRevenueGreater Than or Equal To1000000. - Select Fast Field Updates as the optimization.
- Add an Update Records element on
$Record(the triggering record) that setsRatingtoHot.
Choose after-save when you need related records, emails, Apex actions, or the new record's Id. For example, creating a follow-up Task owned by the account owner needs after-save.
Common errors
- Using after-save for a simple field update. This works, but it triggers a second save and re-runs other automation on the object.
- Weak entry conditions. A flow that fires on every update, with no condition, wastes resources. Use entry conditions and the "only when a field is changed" option.
- Updating the triggering record in an after-save flow. This can re-fire the flow or other triggers. Move the change to a before-save flow where you can.
- Expecting a before-save flow to create a child record. The element is not available there. Use after-save.
- Putting Get Records or Update Records in a loop. These elements still count against governor limits. Collect records in a collection and update once, outside the loop.
Exercise
A requirement says that when an Opportunity moves to Closed Won, set a custom field "Closed Date Stamp" on that Opportunity and create a renewal Opportunity. Which flow timing or timings do you need, and why?
Show answer
Use both, or one after-save flow. Setting the field on the same record fits a before-save flow, which is faster and needs no extra save. Creating the renewal Opportunity touches another record, so it needs an after-save flow. If you want a single flow, after-save can do both, but the field update then causes a second save.