Sub topic 6
After-Save Flows: Related Records and Actions
This chapter covers after-save record-triggered flows. These flows run once the triggering record has been written to the database, so they can create or update other records and call actions. It assumes you know the Salesforce data model (objects, fields, lookups), the main Flow Builder elements (Assignment, Decision, Get Records, Create Records, Update Records), and basic formula logic. It also assumes you know that a transaction is a set of changes that all commit together or all roll back.
What "after save" means
An after-save flow is a record-triggered flow whose Optimize the Flow for setting is Actions and Related Records. Salesforce runs it after the triggering record has been saved to the database, but before the transaction commits.
Two consequences follow from that timing:
- The record has an
Id, even on create. You can use{!$Record.Id}to point a new related record at it. - The saved values are visible to everything the flow does afterward, including formula fields on the record, which have been calculated by this point.
The record is not yet committed. If a later step in the transaction fails, the whole transaction rolls back, including the changes your flow made.
What after-save flows can do
A before-save flow can only change fields on the triggering record. An after-save flow can do much more:
- Create, update, and delete other records, including records of other objects.
- Update the triggering record itself, through an Update Records element.
- Call actions: send email, post to Chatter, submit for approval, invoke Apex actions, run subflows, and send outbound messages or platform events.
- Use Get Records, loops, decisions, and collection variables to work through related data.
- Use asynchronous and scheduled paths, which are covered in a later chapter.
Configuring an after-save flow
The setup is the same as for any record-triggered flow, with the optimization set to Actions and Related Records.
- In Flow Builder, create a new flow and choose Record-Triggered Flow.
- Choose the object and the trigger event (a record is created, updated, created or updated, or deleted).
- Set entry conditions to limit when the flow runs.
- Under Optimize the Flow for, choose Actions and Related Records.
- Add elements to the canvas.
Here is a typical configuration. It creates a follow-up task when an Opportunity becomes Closed Won:
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}The setting Only when a record is updated to meet the condition requirements matters here. The flow runs only when the record changes from not matching the condition to matching it. If someone edits a Closed Won Opportunity later, the flow does not run again and does not create a second task.
If you need more precision than a simple condition, use Formula Evaluates to True. This formula fires only when the stage has just changed to Closed Won:
AND(
ISCHANGED({!$Record.StageName}),
{!$Record.StageName} = "Closed Won"
)ISCHANGED works in flow formulas on record-triggered flows because the flow can compare the record against its prior values. On a create, there is no prior value, so a field that is populated on insert counts as changed.
Working with the triggering record
In an after-save flow, {!$Record} holds the record as it was saved. {!$Record__Prior} holds the values from before the update. On create, $Record__Prior is empty, and on delete there is no $Record after the fact in the usual sense, so plan delete flows around prior values.
A common mistake is to try to change $Record fields with an Assignment element and expect them to persist. In an after-save flow, that does not write anything to the database. The record is already saved. If you need to change the triggering record, you have two options:
- Move that logic to a before-save flow, which is cheaper.
- Use an Update Records element on the triggering record. This is an extra DML operation and causes the record to be saved again, which can re-fire triggers and flows.
Creating related records
The most common after-save pattern is to create a child or related record. You use Create Records and fill in the lookup or master-detail field with the Id of the triggering record.
Key points:
- Use
{!$Record.Id}for the relationship field. In an after-save flow it is always populated. - Set required fields explicitly. Create Records fails if a required field, such as a Task's
Subjecton some orgs or an Opportunity'sStageName, is missing. - Ownership does not follow the parent automatically for lookup relationships. Set
OwnerIdyourself, as the example above does. - To create several records, build a collection variable, add records to it in a loop with an Assignment element, then use a single Create Records element with the collection after the loop. This keeps the flow to one DML statement.
Updating related records
There are two ways to update related records.
Get Records, then Update Records. Look up the records you need, then update them. For example, to close all open Tasks when an Opportunity closes, use Get Records on Task where WhatId = {!$Record.Id} and IsClosed = false. Then use Update Records with the field values you want and the Get Records output as the target.
Update Records with criteria. Update Records can find the records itself using filter conditions and set field values, without a separate Get Records. This works well when the same values go onto every matching record. It uses one query and one DML operation and does not load the records into the flow.
For records related through a parent lookup, such as updating the Account from a Contact flow, use {!$Record.AccountId} as the match condition. You can also read parent fields directly through the record variable, for example {!$Record.Account.Name}, without a Get Records element.
Calling actions
Actions are the second reason to choose after-save. Common actions include:
| Action | Typical use |
|---|---|
| Send Email | Notify an owner or customer when a condition is met |
| Post to Chatter | Announce a status change to a group |
| Submit for Approval | Start an approval process on the record |
| Invocable Apex | Run logic that Flow cannot express |
| Subflow | Reuse a shared piece of logic |
| Publish platform event | Notify external systems or other automation |
Actions run in the same transaction, so a failure in one can roll back the flow's earlier changes unless you handle it with a fault path.
Where the flow runs in the transaction
An after-save flow runs after before-save flows, before-save Apex triggers, system validation, and custom validation rules. The record has been written to the database but not committed. Roll-up summary fields and other processing that occurs after the save then follow. The details are covered in the order-of-execution chapter.
Bulk behavior and limits
Flows run in bulk. When a data load or API call saves 200 records, Salesforce passes them to the flow as a batch, and elements such as Get Records, Create Records, and Update Records run against the whole batch. You do not write a loop over the triggering records. Flow handles that for you.
You must still protect the limits that your own loops can break:
- Do not put Get Records, Create Records, Update Records, or Delete Records elements inside a loop. Each one is a separate query or DML statement, and you can hit the limit of 100 SOQL queries or 150 DML statements per transaction.
- Build up a collection inside the loop, then run one DML element after the loop.
- Remember that limits are shared with Apex and every other automation in the same transaction, not counted per flow.
Recursion and re-triggering
After-save flows are the usual source of unintended re-triggering. If a flow on Opportunity updates the same Opportunity, the second save can run the flow again. If a flow on Opportunity updates an Account and a flow on Account updates the Opportunity, the two can call each other until the transaction hits a limit.
To prevent this:
- Use Only when a record is updated to meet the condition requirements so the flow fires on a change into the condition, not on every save while it is true.
- Use
ISCHANGEDor compare$Recordwith$Record__Priorin a formula entry condition. - Avoid updating the triggering record from an after-save flow when a before-save flow could do the work.
- Include a guard field or condition that becomes false after the flow's own update.
Fault handling
Create, Update, Delete, and Get Records elements, and most actions, have a Fault connector. If you do not add one, an error stops the flow and rolls back the transaction, and the user sees the error. A fault path lets you log the error, send a notification, or show a custom message. Use {!$Flow.FaultMessage} to read the error text.
If you route a fault to a path that ends normally, the transaction can still commit with the earlier changes in place. Decide deliberately whether you want that outcome or a rollback.
Testing the example
Because the flow runs in the same transaction as the DML, an Apex test can insert or update a record, then query the results. This test assumes the after-save flow above is active:
@IsTest
private class OpportunityFollowUpTaskTest {
@IsTest
static void closedWonCreatesTask() {
Opportunity opp = new Opportunity(
Name = 'Test deal',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30)
);
insert opp;
opp.StageName = 'Closed Won';
update opp;
List<Task> tasks = [
SELECT Subject FROM Task WHERE WhatId = :opp.Id
];
Assert.areEqual(1, tasks.size());
Assert.areEqual('Send welcome package', tasks[0].Subject);
}
}The test checks for exactly one task. If the entry condition were loose, a second update could create a duplicate and the test would catch it when extended with another update.
Common errors
- Assigning to
$Recordin an after-save flow and expecting it to save. Use a before-save flow, or an Update Records element. - Leaving out required fields on Create Records. The flow fails at runtime with a required-field error.
- DML inside loops. The flow works with one record and fails on a bulk load.
- Entry conditions that stay true. The flow runs on every later edit and creates duplicates.
- No fault path. Users see raw error text, and nobody is notified.
Exercise
An Opportunity flow is set to Actions and Related Records, runs when a record is created or updated, and has the entry condition StageName = "Closed Won". Its run setting is "Every time a record is updated and meets the condition requirements". Users report duplicate follow-up Tasks on Closed Won Opportunities. What causes this, and what change fixes it?
Show answer
With "Every time a record is updated and meets the condition requirements", the flow runs on every save while the stage is Closed Won, such as when someone edits the Amount. Each run creates a Task. Change the run setting to "Only when a record is updated to meet the condition requirements", or use a formula entry condition with ISCHANGED({!$Record.StageName}) so the flow runs only when the stage changes to Closed Won.
Exercise
Why can't an after-save flow update a field on the triggering record by using an Assignment element on {!$Record}, and what are two ways to handle the requirement?
Show answer
The record is already saved when an after-save flow runs, so changes made with an Assignment element on $Record are not written to the database. Either move the change to a before-save flow, which updates the record without extra DML, or use an Update Records element on the triggering record, accepting the extra DML and a second save that can re-fire automation.