Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Automation with Flow

Record-Triggered Flows: Before-Save and After-Save

Build Salesforce record-triggered flows: trigger conditions, fast field updates versus actions and related records, entry criteria, order of execution, with a JavaScript stand-in.

A record-triggered flow runs automatically when someone (or something) creates, updates or deletes a record. It is the workhorse of Salesforce automation: stamp a date when an opportunity closes, create a follow-up task when a case escalates, copy an address from an account to its contacts. Because it runs without any user clicking anything, it must be designed carefully: it fires for data loads, integrations and other flows too, not only for people using the screen.

This lesson explains how to configure the trigger, what the two run timings mean, and how a flow fits into the order of execution. As in this whole chapter, the Salesforce menus are described in words, and the code is a labelled JavaScript stand-in.

Configure the trigger

When you create a record-triggered flow, you choose:

  1. Object: for example Opportunity.
  2. Trigger: A record is created, A record is updated, A record is created or updated, or A record is deleted.
  3. Entry conditions: the filter that decides whether the flow runs for this particular record. Use All Conditions Are Met (AND), Any (OR), Custom logic or a formula evaluates to true.
  4. Optimize the flow for: Fast Field Updates (before the record is saved) or Actions and Related Records (after the record is saved).

For update triggers, there is one more setting: Run the flow only when a record is updated to meet the condition requirements. Without it, the flow runs on every save of a record that meets the conditions. With it, the flow runs only when the record changes from not meeting the conditions to meeting them. This is how you say "when the stage becomes Closed Won" rather than "every time a Closed Won opportunity is edited".

Before-save (fast field updates)

A before-save flow runs before the record is written to the database. It can change fields on the triggering record only, using {!$Record} as the target. Because no extra save is needed, it is very fast, roughly an order of magnitude cheaper than an after-save update to the same field. Use it for:

  • setting or normalizing a field (trim a name, set a default, calculate a value),
  • copying a value from a parent that you fetched with Get Records.

You cannot create other records, send emails or call actions in a before-save flow. In the before-save flow you also do not use an Update Records element on $Record: you simply use an Assignment to set the field on {!$Record}, and the platform saves it with the record.

An after-save flow runs once the record exists in the database (it has an Id, even on create). Use it when you need to:

  • create or update other records (a task, a related contact, the parent account),
  • send emails, post to Chatter, submit for approval,
  • call a subflow or an Apex action,
  • send an outbound message through an action.

If an after-save flow wants to change the triggering record itself, it must use an Update Records element, which causes another save (and another round of triggers and flows). That is slower and can cause loops, so prefer before-save for same-record changes.

flowchart LR
  A["User saves record"] --> B["Validation (system)"]
  B --> C["Before-save flows"]
  C --> D["Before triggers (Apex)"]
  D --> E["Record written to database"]
  E --> F["After triggers (Apex)"]
  F --> G["After-save flows"]
  G --> H["Commit, then post-commit logic (emails)"]

A simplified order of execution. Real saves have more steps; the key point is where each flow type sits.

Avoiding infinite loops

If an after-save flow updates a record, that update can fire the same flow again. Protect yourself:

  • Use entry conditions that are false after the flow has done its work (for example, run only if Needs_Review__c is true, and have the flow set it to false).
  • Use "only when a record is updated to meet the condition requirements".
  • Check {!$Record__Prior} (the previous version of the record) to react only to a real change, for example {!$Record.StageName} <> {!$Record__Prior.StageName}.

Salesforce also stops runaway recursion with governor limits, but by then the user sees an error, so design the stop condition yourself.

Bulk safety

A data load can save 200 records in one batch, and your flow runs for all of them in one transaction. Flow Builder handles the bulk for you if you follow one rule: never put a Get Records, Create, Update or Delete element inside a Loop. Put the changed records in a collection, and do one data operation after the loop. The next lessons return to this (Loops, collections and limits).

Stand-in: entry conditions and prior values

This stand-in is not Salesforce. It models "run only when the record is updated to meet the conditions" by comparing the old and new versions of a record.

JS Stand-in: only when the stage changes to Closed Won

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Save 2 is the classic mistake: an edit to an already-won opportunity. Without the "becomes true" option the flow would fire again and, for example, create a second onboarding task.

Stand-in: before-save versus after-save

JS Stand-in: count the saves

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Practical guidelines

  • One flow per object and trigger where you can, with Decision elements for the cases. Multiple flows are allowed; use Trigger Order (a priority number in the flow's Advanced settings) if their order matters.
  • Name flows by object and purpose: Opportunity - After Save - Closed Won Onboarding.
  • Document each flow in its Description, including its entry conditions.
  • Exclude integrations if needed: add a condition on a field like Skip_Automation__c, or check the running user, so data loads do not trigger emails.
  • Remember that deleting triggers see only {!$Record} as the record being deleted (before-delete or after-delete flows exist in newer releases).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which timing is fastest for changing a field on the same record that triggered the flow?

    Before-save changes are written with the record, so no extra save is needed.
  2. 2. You need to create a Task related to the opportunity when it is won. Which option do you choose?

    Creating other records requires the record to exist, which means an after-save flow.
  3. 3. What does 'run only when a record is updated to meet the condition requirements' prevent?

    The flow runs only when the record changes from not meeting to meeting the conditions.
  4. 4. What is {!$Record__Prior}?

    It holds the previous version of the record so you can detect real changes.

Summary

  • A record-triggered flow starts from a create, update or delete of one object, filtered by entry conditions.
  • Before-save flows change only the triggering record and are very fast; after-save flows can touch other records and call actions.
  • Use "becomes true" and $Record__Prior to avoid repeat runs and loops.
  • Design for bulk: no data elements inside loops.

Next: Screen flows, variables and formulas