Sub topic 4
Reading current and prior values with $Record and $Record__Prior
This chapter assumes you can build a basic flow in Flow Builder, write a formula with Boolean logic, and know the difference between a field value and a record Id. You should also know that a record-triggered flow runs inside a database transaction. Here you will learn how a flow reads the record that started it, and how it reads what that record looked like before the change.
The two global variables
Every record-triggered flow gets two special variables when it starts. You do not create them. Flow Builder supplies them.
$Recordis a record variable holding the triggering record as it exists now, with the new values from the save.$Record__Prioris a record variable holding the triggering record as it was before the save, with the old values.
You can use both in entry conditions, Decision elements, formulas, Assignment elements, and the inputs of other elements.
The values you see depend on the trigger event.
| Trigger event | `$Record` | `$Record__Prior` |
|---|---|---|
| Created | The new record | Not available. There is no earlier version. |
| Updated | The record with the new values | The record with the old values |
| Created or updated | The new record, or the updated record | Populated only on an update. Empty on a create. |
| Deleted | The record being deleted | Not available. |
On a delete, $Record holds the values the record had when it was deleted. This is useful for audit actions, such as copying key fields into a log record.
Reading field values
Use dot notation to reach a field: {!$Record.StageName} or {!$Record__Prior.StageName}. You can also traverse a lookup relationship, for example {!$Record.Account.Name} or {!$Record.Owner.Email}. This reads the parent record without a Get Records element.
Relationship traversal has limits worth knowing:
- Traversal goes up to a parent through lookup or master-detail fields. It does not go down to child records. To read children you need a Get Records element.
- Cross-object values are read-only in a before-save flow. You can read
{!$Record.Account.Industry}, but you cannot assign a value to it. - A related value that is blank in the database comes back as null. Always allow for that in a formula.
Detecting a change
The most common reason to use $Record__Prior is to find out whether a field changed. There are two ways to do it, and they behave the same in normal cases.
The first compares the two variables directly:
{!$Record.StageName} <> {!$Record__Prior.StageName}The second uses the ISCHANGED function in a formula:
ISCHANGED({!$Record.StageName})ISCHANGED is shorter and states the intent. It is also the pattern used in the entry condition from the topic's first example, which lets an Opportunity enter the flow only when its stage has just changed to Closed Won:
AND(
ISCHANGED({!$Record.StageName}),
{!$Record.StageName} = "Closed Won"
)The direct comparison has an advantage in a Decision element, where you may want to compare a field to a specific prior value, such as "was Prospecting, is now Qualified."
AND(
{!$Record__Prior.StageName} = "Prospecting",
{!$Record.StageName} = "Qualified"
)Two other related functions exist for formulas: ISNEW() is true when the record is being created, and PRIORVALUE() returns the old value of a field. In flow formulas, prefer $Record__Prior over PRIORVALUE, because it is easier to read and works the same way in Decision elements.
Why entry conditions are better than Decision elements
You can test $Record__Prior in two places: the entry condition of the flow, and a Decision element inside it. The results can look the same, but the cost differs.
If the entry condition is false, the flow never starts. No interview is created and nothing counts against the transaction. If you start the flow for every update and filter in a Decision element, every update spends the overhead of starting an interview. On an object with heavy data loads, that adds up.
Put the filter in the entry condition when you can. Use a Decision element when you need several branches that share the same trigger.
The entry condition also controls how often the flow runs for a given record. When you choose Run: Only when a record is updated to meet the condition requirements, Salesforce evaluates the condition against the prior record too. The flow fires only if the prior record did not meet the condition and the new one does. This is a built-in form of change detection, and it is often enough that you do not need $Record__Prior at all.
A worked example
You want the flow to react when the Amount on an Opportunity increases by more than 50,000 in one edit, and to record that jump in the Next Step field.
Configure the flow:
Flow: Opportunity - Flag Big Increase (before-save)
Object: Opportunity
Trigger: A record is updated
Optimize for: Fast Field Updates
Entry condition: Formula Evaluates to True
AND(
NOT(ISBLANK({!$Record__Prior.Amount})),
NOT(ISBLANK({!$Record.Amount})),
{!$Record.Amount} - {!$Record__Prior.Amount} > 50000
)
Assignment element
Variable: {!$Record.NextStep}
Operator: Equals
Value: Large increase, reviewThe formula checks for blank values first. In a formula, subtracting a blank number can produce a null or an unexpected result, so the guards keep the condition predictable. The trigger is set to A record is updated because $Record__Prior is not available on a create.
Because the flow is before-save, the Assignment element changes $Record directly. No Update Records element is needed, and Salesforce saves the change as part of the original save.
`$Record` in before-save and after-save
The same variable behaves differently depending on the optimization.
In a before-save flow, $Record is the in-memory record that has not yet been written. You change its fields with Assignment elements, and those changes are saved with the record. Nothing is committed yet, so {!$Record.Id} is empty on a create. The Id does not exist until the save.
In an after-save flow, the record is already written, so {!$Record.Id} is available. This is why the after-save example in the plan can create a related Task with WhatId: {!$Record.Id}. In an after-save flow you cannot assign directly to $Record fields. To change the triggering record you must use an Update Records element that targets {!$Record}, and that second save can re-trigger automation. That topic is covered in the recursion chapter.
| Question | Before-save | After-save |
|---|---|---|
| Is `$Record.Id` set on create? | No | Yes |
| Can you assign to `$Record` fields? | Yes, with Assignment | No, use Update Records |
| Is `$Record__Prior` available on update? | Yes | Yes |
| Can you change the record without extra DML? | Yes | No |
What `$Record__Prior` does not give you
$Record__Prior holds the state before the current save. Keep these limits in mind.
- It reflects the record at the start of the current save operation, not the record from some earlier transaction. It is not a history of changes.
- If another automation in the same transaction changes the record before your flow runs, your flow sees that change in
$Record. Whether$Record__Priorshows the original or the intermediate value depends on where your flow sits in the order of execution. Test this case if you have several automations on one object. - It cannot be modified. Assigning to
$Record__Priorfields is not allowed. - It is not defined for create or delete events, so a formula that reads it on those events will not behave as you expect. Use
ISNEW()or a separate trigger configuration to cover creates.
Common errors
- Reading
$Record__Prioron a create. The condition returns null or false and the flow never fires. Use an update trigger or guard withISNEW(). - Comparing null values.
{!$Record.Amount} > {!$Record__Prior.Amount}fails to behave as intended when either side is blank. AddISBLANKchecks. - Using
{!$Record.Id}in a before-save create. The Id is not set yet. Move the logic to an after-save flow. - Assigning to
$Recordin an after-save flow. This is not allowed. Use Update Records instead, and check that the update does not loop. - Treating
ISCHANGEDas a time window. It compares only the current save with the one just before it. It says nothing about earlier edits. - Filtering in a Decision element when an entry condition would do. The flow starts for every record, which wastes processing.
Checking your understanding
Before you move on, a small test of the concepts. The Apex test below assumes the before-save flow from the topic's earlier example is active. It inserts a large Opportunity and checks that the flow set Next Step. Notice that the flow reads $Record.Amount on a create, not $Record__Prior.
@IsTest
private class OpportunityFlowTest {
@IsTest
static void beforeSaveFlowSetsNextStep() {
Opportunity opp = new Opportunity(
Name = 'Large deal',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
Amount = 200000
);
insert opp;
Opportunity saved = [SELECT NextStep FROM Opportunity WHERE Id = :opp.Id];
Assert.areEqual('Review large deal', saved.NextStep);
}
}Exercise
An Opportunity flow is set to run when a record is created or updated. Its entry condition is {!$Record.Amount} > {!$Record__Prior.Amount}. Users report that new Opportunities never trigger the flow, even with a large Amount. Explain why, and give a corrected entry condition that still handles updates properly.
Show answer
On a create there is no prior record, so $Record__Prior.Amount is null and the comparison does not evaluate to true. The flow never fires for new records. A corrected condition that handles both cases is OR(ISNEW(), AND(NOT(ISBLANK({!$Record__Prior.Amount})), {!$Record.Amount} > {!$Record__Prior.Amount})). This lets every create through and, on updates, requires the Amount to have increased from a non-blank prior value. If you only wanted updates, setting the trigger to "A record is updated" would be simpler.
Exercise
You need to create a Task related to a new Opportunity, using the Opportunity's Id as WhatId. Should this be a before-save or an after-save flow, and why? What happens if you try to read {!$Record.Id} in the other type on a create?
Show answer
Use an after-save flow. The Id is assigned only when the record is saved, and the after-save flow runs after that save, so {!$Record.Id} is populated. A before-save flow runs before the write, so {!$Record.Id} is empty on a create, and the Task would have no valid parent.