Sub topic 11
After-Save Flows
This chapter assumes you can write a basic Apex trigger, know how validation rules work, and have built a simple record-triggered flow. It covers where after-save flows run in the order of execution and what they can safely do.
Where after-save flows run
An after-save flow is a record-triggered flow set to run "Actions and Related Records". It runs after the record has been written to the database, but before the transaction commits. The flow therefore sees the record's Id, including on insert. That is the main reason to choose it over a before-save flow.
In the order of execution, the flow runs after the after triggers, after assignment, auto-response, workflow, and escalation rules, and after any workflow field update has caused the triggers to fire again. Roll-up summary calculations and criteria-based sharing evaluation come next, and then the commit.
What an after-save flow can do
Use an after-save flow when you need one of these:
- The new record's
Id, for example to create a child record that points to it. - To create or update related records, or to run actions such as sending an email or calling a subflow.
- To change other objects based on the saved record.
Because the record is already saved, you cannot change its own fields by assigning values the way a before-save flow does. To update the triggering record, you need an Update Records element. That element is a DML operation and runs the record's save sequence again.
Re-entry through the flow
When a flow updates records, those saves go through the full order of execution. Each of them can fire before triggers, after triggers, validation rules, and other flows. If a flow updates the record that started it, the object's triggers and flows can run a second time on that same record.
The three common results of this are:
- A record update causes the flow to run again, and the flow loops until a limit is hit.
- A trigger runs twice, so its work is duplicated.
- Governor limits are used up faster than expected, because the flow and the triggers share them.
To prevent this, set the flow's entry conditions so it only runs when a relevant field changes, such as when the status becomes "Closed" and was not "Closed" before. Use the "Only when a record is updated to meet the condition requirements" option. Avoid updating the triggering record from an after-save flow when a before-save flow could do the same job.
Errors and limits
An after-save flow shares the transaction with the triggers around it. If the flow fails with an unhandled fault, the whole save is rolled back, including the record itself and any work the triggers did. The flow also counts toward the same governor limits, such as the 150 DML statements and 100 SOQL queries per synchronous transaction. Flows process records in bulk, but a Get Records or Update Records element placed inside a loop can still use many queries or DML statements.
Choosing between flow types
| Need | Use |
|---|---|
| Set a field on the record being saved | Before-save flow |
| Use the new record's `Id` | After-save flow or after trigger |
| Create or update related records | After-save flow or after trigger |
| Run logic after the commit, such as a callout | Asynchronous path in the flow |
Exercise
A flow must create a Task linked to a new Account the moment the Account is inserted. Which type of record-triggered flow should you use, and why?
Show answer
An after-save flow, because the Account's Id does not exist until the record has been saved. A before-save flow cannot set the Task's WhatId to a record that has no Id yet.