Sub topic 5
Record-triggered flows: before-save versus after-save
This chapter assumes you know the Salesforce data model and can build a basic flow in Flow Builder. It also assumes you have seen the idea of a trigger firing when a record is saved. By the end, you should be able to choose between the two optimization modes of a record-triggered flow and avoid the most common mistakes in each.
What a record-triggered flow is
A record-triggered flow starts automatically when a record is created, updated, or deleted. You choose:
- The object that starts the flow.
- The trigger event: created, updated, created or updated, or deleted.
- Entry conditions that limit which records start the flow.
- What the flow is optimized for.
The last choice is the one this chapter covers. Fast Field Updates gives you a before-save flow. Actions and Related Records gives you an after-save flow. You cannot switch a flow between the two modes after you have built elements in it. Pick the mode first.
Before-save flows: Fast Field Updates
A before-save flow runs before the record is written to the database. It changes the record in memory, and Salesforce then saves those changes as part of the original save.
Because of this, a before-save flow needs no separate update step. You set values on $Record with an Assignment element. This avoids an extra DML operation (a database write) and avoids re-running other automation on the record. That makes before-save flows much cheaper than after-save flows.
A before-save flow can:
- Set fields on the triggering record.
- Read other records with Get Records.
- Use Decision, Assignment, Loop, and Formula resources.
- Compare the new values with the old ones through
$Record__Prioron updates.
A before-save flow cannot:
- Create, update, or delete other records.
- Call actions such as sending email, posting to Chatter, or calling Apex.
- Call subflows.
- Read the new record's Id on create, because the record has not been saved yet.
The Apex trigger from the topic plan does the same job as a before-save flow with one Decision and one Assignment:
trigger AccountTrigger on Account (before insert, before update) {
for (Account acc : Trigger.new) {
if (acc.AnnualRevenue != null && acc.AnnualRevenue >= 1000000) {
acc.Rating = 'Hot';
}
}
}In a flow, the entry condition would be AnnualRevenue >= 1000000, and one Assignment would set $Record.Rating to Hot. There is no code to test or deploy, and an admin can maintain it. This is the clicks-before-code principle applied to a single record.
After-save flows: Actions and Related Records
An after-save flow runs after the record has been saved to the database, but before the transaction commits. The record now has an Id, and you can use it in other records.
Use an after-save flow when you need to:
- Create or update related records, such as creating a Task when an Opportunity closes.
- Update the triggering record in a way that depends on its Id or on saved values.
- Send email, post to Chatter, or call an invocable action or Apex.
- Launch a subflow.
- Run a scheduled path or an asynchronous path.
After-save flows cost more. Every Create, Update, or Delete element is a DML operation that counts against the transaction's governor limits. Those limits are shared with any Apex triggers and other automation in the same transaction.
Comparing the two modes
| Question | Before-save | After-save |
|---|---|---|
| When does it run? | Before the record is written | After the record is written, before commit |
| Can it change the triggering record? | Yes, by assignment, with no DML element | Yes, but with an Update Records element (extra DML) |
| Can it change other records? | No | Yes |
| Is the new record's Id available? | No, not on create | Yes |
| Can it send email or call actions? | No | Yes |
| Can it use scheduled or asynchronous paths? | No | Yes |
| Relative cost | Low | Higher |
Choosing between them
A practical rule: if the requirement is "when this record is saved, set or correct a field on the same record," use before-save. Otherwise use after-save.
A requirement can need both. For example, you might stamp a field on the record and also create a follow-up Task. Build two flows on the object: a before-save flow for the field and an after-save flow for the Task. Do not put the field change in the after-save flow just for convenience. Doing so adds a DML operation and re-runs automation on the record.
Common errors
Updating the triggering record in an after-save flow
If an after-save flow updates $Record, Salesforce saves the record a second time. The save can re-run triggers and flows on the same object. Used carelessly, this causes extra work or loops. To reduce the risk:
- Move the change into a before-save flow when you can.
- Set entry conditions that stop the flow from matching again after its own update.
- Use the option that runs the flow only when a record is updated to meet the condition requirements, so it does not fire on every later edit.
DML or queries inside loops
Flow is bulkified by the platform. When many records are saved together, such as in a data load, the flow interviews are batched. You can still break this by putting Get Records, Create Records, or Update Records inside a Loop. Collect changes in a collection variable inside the loop, then write once after the loop.
Weak entry conditions
A flow with no entry condition runs on every save of the object. Always filter, and use the "changed" check when you only care about a field changing:
- The flow runs only when
StageNameequalsClosed Won. - The flow runs only when
StageNamediffers from$Record__Prior.StageName.
Wrong expectations about order
Before-save flows run early in the order of execution, before Apex before-triggers. After-save flows run late, after after-triggers and workflow rules. A later chapter covers the full sequence. For now, remember that a before-save flow sees the record before Apex has touched it, and an after-save flow sees the record after Apex has.
Summary
- Before-save: fast, same record only, no actions, no Id on create.
- After-save: related records, actions, subflows, and async or scheduled paths, at higher cost.
- Choose before-save by default for same-record field changes, and add an after-save flow for anything else.
Exercise
A requirement says: "When an Opportunity moves to Closed Won, set its Description field to a standard note and create a renewal Task for the owner." Which flow mode or modes would you use, and why?
Show answer
Use two flows. A before-save flow sets the Description field with an assignment on $Record, because it only changes the triggering record and needs no extra DML. An after-save flow creates the Task, because it creates a related record and needs the saved Opportunity's Id for WhatId. Both have an entry condition on StageName changing to Closed Won, so they do not run on every save.