Sub topic 6
After-save flows: related records and actions
This chapter covers how an after-save flow works and what it can do that a before-save flow cannot. It assumes you know the Flow Builder elements (Assignment, Decision, Get Records, Create Records, Update Records) and what a database transaction is. It also assumes you have read the earlier chapters on trigger configuration, entry conditions, and $Record.
What "after-save" means
An after-save flow runs after Salesforce has written the triggering record to the database, but before the transaction commits. The record already has an Id, and system fields such as CreatedDate are populated. In Flow Builder you select Actions and Related Records under Optimize the Flow for.
Because the record is already saved, the flow reaches beyond it. You can:
- Create, update, or delete other records.
- Run actions such as sending email, posting to Chatter, or calling an invocable Apex action.
- Call subflows.
- Use asynchronous and scheduled paths (covered in a later chapter).
Why the Id matters
A before-save flow on a new record has no {!$Record.Id}. An after-save flow does, so it can link child records to the parent. The following flow creates a follow-up Task when an Opportunity reaches 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}WhatId points the Task at the Opportunity. The run setting "Only when a record is updated to meet the condition requirements" means the flow fires when the record starts matching the condition. A later edit to an already Closed Won Opportunity does not create a second Task.
Updating the triggering record
You can still change the triggering record in an after-save flow, but you must use an Update Records element. Set it to update the record that triggered the flow, using {!$Record}. This causes a second save, which costs a DML statement and can re-fire triggers and flows on the same object. A before-save Assignment avoids that cost. If the only goal is to set fields on the triggering record, choose before-save.
Data flow in an after-save flow
Everything in the diagram sits in one transaction. If the flow fails and the error is not handled, the whole transaction rolls back, including the Opportunity save.
Common errors
- Using after-save to set fields on the triggering record. It works, but it adds a DML statement and may cause recursion. Use before-save.
- Creating records inside a loop. Build a collection variable in the loop. Then use one Create Records element after the loop. Salesforce bulkifies the flow's elements across a batch of records, but a DML element inside a loop still consumes limits per iteration.
- Missing entry conditions. Without them, the flow runs for every save and wastes limits. Put the condition in the Start element, not in a Decision after it.
- Ignoring shared limits. The flow shares the transaction's governor limits with Apex triggers and other automation. A flow that runs near the limit alone can fail once other automation is added.
- No fault handling. An unhandled failure in a Create Records element rolls back the user's save. Add a fault path for any element that changes data.
Testing
Open the flow in Flow Builder and use Debug on a sample record. For automated checks, insert or update a record in an Apex test and query for the related record. Active flows run during Apex tests, so the test exercises them. Query the Task that the flow above creates:
@IsTest
private class OpportunityTaskFlowTest {
@IsTest
static void closedWonCreatesTask() {
Opportunity opp = new Opportunity(
Name = 'Won 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 assumes the after-save flow above is active in the org.
Exercise
A flow must set a Task's Description field when the Task is created, and also create a related Case. Which optimization can do both, and why?
Show answer
After-save (Actions and Related Records). Creating a Case is a related-record change, which before-save flows cannot do. The Description on the triggering Task could be set in an after-save flow only through an Update Records element, which costs an extra save. Alternatively, use two flows: a before-save flow for the field and an after-save flow for the Case.
Exercise
Why can an after-save flow use {!$Record.Id} when creating a child record for a newly inserted Opportunity, but a before-save flow cannot?
Show answer
The after-save flow runs after the record is written to the database, so the Id already exists. A before-save flow runs before the insert, so a new record has no Id yet.