Sub topic 4
Before-save flows run first
This chapter assumes you can write a basic Apex trigger, know Salesforce objects and fields, and have seen validation rules and Flow. Earlier chapters covered the save as one transaction, loading the record, and system validation. Here we look at the first automation step that can change field values.
What a before-save flow is
A before-save flow is a record-triggered flow that runs before the record is written to the database. It is the declarative counterpart of a before trigger. Because the record has not been saved yet, the flow can change the triggering record's own fields without a DML operation. Salesforce simply writes the changed values with the rest of the save.
In the order of execution, these flows run before Apex before triggers. Anything the flow sets is therefore visible to your before trigger.
What you can and cannot do
Before-save flows are fast because they skip the extra work of a separate update. That speed comes with limits.
| Capability | Before-save flow |
|---|---|
| Change fields on the triggering record | Yes, with no DML |
| Read the record's Id on insert | No, the Id does not exist yet |
| Create or update related records | No, use an after-save flow or trigger |
| Send emails or call actions | No |
Because the Id is missing on insert, a before-save flow cannot link a new child record to the record being created. That work belongs after the save.
Example: setting a default
Suppose you want every new Account to have a description. A before-save flow can set it. Its configuration is:
- Create a record-triggered flow on Account.
- Trigger it when a record is created or updated.
- Choose Fast Field Updates, which is the before-save option.
- Add an entry condition:
Descriptionis null. - Use an Update Records element to set
DescriptiontoSet in flow.
Now consider a before trigger on the same object.
trigger AccountDescription on Account (before insert, before update) {
for (Account a : Trigger.new) {
System.debug('Description seen by trigger: ' + a.Description);
}
}When you insert an Account without a description, the debug line shows Set in flow. The flow ran first, so the trigger sees the new value.
Common errors
- Expecting the Id. A before-save flow on insert cannot use the record Id. Move that logic to an after-save flow.
- Overwriting the flow in a trigger. A before trigger runs later and can replace the flow's value. If the final value is wrong, check both.
- Assuming validation rules have run. Custom validation rules run after before triggers. The flow can set a value that a validation rule then rejects.
- Missing entry conditions. Without conditions, the flow runs on every save and can overwrite user input.
Exercise
A before-save flow and a before trigger both set Description on the same new Account. Which value is saved, and why?
Show answer
The before trigger's value is saved. Before-save flows run first, and the before trigger runs afterward and overwrites the field. A custom validation rule could still reject the final value later.
Exercise
You need to create a Task related to a new Account in a flow. Can a before-save flow do this? Why or why not?
Show answer
No. On insert the Account has no Id yet, and before-save flows cannot create related records. Use an after-save flow, which runs once the record is saved and has an Id.