Sub topic 7
Choosing between before-save and after-save
This chapter assumes you can build a basic record-triggered flow. You should know the Assignment, Decision, Get Records, Create Records and Update Records elements. You should also know how to set the object, trigger event and entry conditions, and how a database transaction groups work into one commit. Here you learn how to pick the right optimization for a requirement, and what you give up when you pick the wrong one.
The two options at a glance
When you create a record-triggered flow, Flow Builder asks what to optimize the flow for.
- Fast Field Updates creates a before-save flow. It runs before the record is written to the database.
- Actions and Related Records creates an after-save flow. It runs after the record is written, but before the transaction commits.
The choice is made when you create the flow. If you pick wrong, you usually rebuild the flow.
| Capability | Before-save (Fast Field Updates) | After-save (Actions and Related Records) |
|---|---|---|
| Change fields on the triggering record | Yes, with an Assignment on `$Record` | Yes, but only with an Update Records element, which is an extra save |
| Create, update or delete other records | No | Yes |
| Call actions (email, Apex invocable, subflow, platform event) | No | Yes |
| `$Record.Id` on a create | Not available, the record has no Id yet | Available |
| Asynchronous and scheduled paths | No | Yes |
| Extra DML on the triggering record | None | One more save if you update `$Record` |
| Relative speed | Fastest option | Slower |
Salesforce documents before-save flows as much faster than comparable after-save automation, because they skip the second save and its extra processing.
A decision procedure
Ask the questions in order. The first "yes" to a question about other records or outside effects forces after-save.
If the flow only changes fields on the record that fired it, choose before-save. If you are unsure, check whether any step in the design needs the database to hold the record first.
Case 1: setting fields on the triggering record
The requirement is that large opportunities get a standard next step. The flow only writes to the record that triggered it, so before-save fits.
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 dealThe Assignment changes the in-memory record before it is saved. The platform then saves it once, with the new value included. There is no Update Records element, no second DML statement, and no second round of triggers and validation. Validation rules and Apex before triggers also see the value the flow set, because the flow ran first.
You can still read other data in a before-save flow. A Get Records element can look up a related record, and $Record.Account.Name can read across a relationship. You can use those values in the Assignment. What you cannot do is write to those other records.
Case 2: creating related records
The requirement is that when an opportunity is closed won, a follow-up task is created for the owner. The flow must write to a different object, so it must be after-save.
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}WhatId needs the opportunity's Id. In an after-save flow the record has already been inserted, so {!$Record.Id} has a value, even on a create. A before-save flow on a create cannot do this, because the Id does not exist yet.
Case 3: both kinds of work in one requirement
Many requirements mix the two. For example: "When an opportunity is closed won, stamp a field with a review message and create a task."
You have two reasonable designs.
- One after-save flow. Use Create Records for the task and an Update Records element on
$Recordfor the field. This is simple, but the field change costs an extra save of the opportunity. That save runs validation, triggers and other flows again. - Two flows. Put the field change in a before-save flow and the task creation in an after-save flow. This avoids the extra save and keeps each flow small. The cost is two flows to maintain and to order, as the later chapter on coexistence explains.
Prefer two flows when the field update is on a high-volume object or when the extra save would re-fire other automation. Prefer one flow when the logic is tightly coupled and volume is low. A field the after-save flow needs to read back can only come from the before-save flow's result if the before-save flow has already run. It does, because before-save runs first in the transaction.
Edge cases and gotchas
Why updating `$Record` in after-save is costly
In an after-save flow, $Record is a read-only copy of what was saved. To change a field on the triggering record, you must use an Update Records element. That element issues a new save of the same record. The save re-runs validation rules, Apex triggers and flows on that object. This is the main source of recursion problems, and the later chapter on recursion covers how to prevent it. Choosing before-save when it is possible avoids the problem.
Deletes
Record-triggered flows can also run on delete. Because the record is being removed, field updates make no sense there. Use a delete-triggered flow for side effects such as blocking the delete with a custom error or cleaning up related records. Check the current release notes for which optimizations your org supports on delete, because the options have changed between releases.
Asynchronous and scheduled paths
Only after-save flows can add an Run Asynchronously path or a scheduled path. If the requirement says "three days after close" or "call an external system without holding up the save", the answer is after-save, whatever else the flow does.
Field-level security and context
Record-triggered flows run in system context by default. A flow can write to fields the running user cannot edit. This applies to both optimizations, so do not use the choice of optimization as a security control.
Limits are shared
Both types run inside the same transaction as the save. After-save flows count their queries, DML statements and other limit usage against the same governor limits as Apex in that transaction. A before-save flow uses fewer, since it does no DML. A flow with a Get Records inside a loop can fail in either type when many records load at once.
A quick check before you commit
Before you build, write down each step of the requirement and mark whether it writes to the triggering record, writes to another record, or has an outside effect. Then apply these rules.
- Every step writes only to the triggering record: before-save.
- Any step writes to another record or has an outside effect: after-save, or split the flow.
- Any step needs the Id on a create, or a delayed or asynchronous path: after-save.
Also check the entry condition. A tight entry condition, such as ISCHANGED({!$Record.StageName}) combined with a stage value, matters more for after-save flows because each unneeded run pays for extra work.
Exercise
A requirement says: "When a Case is created with Priority = High, set Escalated__c to true on that Case, and create a Task for the case owner linked to the Case." Which optimization or optimizations should you use, and why?
Show answer
Use both, or one after-save flow. Setting Escalated_c only changes the triggering record, which a before-save flow can do with no extra save. Creating the Task needs the Case Id for WhatId, and the Id does not exist during a before-save create, so the Task needs an after-save flow. A single after-save flow also works, but the Escalated_c update would cost an extra save of the Case, so two flows is usually better on a busy object.
Exercise
A before-save flow on Opportunity has a Create Records element for a Task, and Flow Builder will not let you add it. What do you do?
Show answer
Before-save flows cannot create or update other records. Rebuild the logic as an after-save flow (Actions and Related Records), which can create the Task and use {!$Record.Id} for WhatId. Keep any field updates on the Opportunity itself in a separate before-save flow if you want to avoid the extra save.