Spring '21 Flows | Is Changed in Record Triggered Flows using Prior Value
Is changed in Flows Salesforce and Prior Value in Flows : Spring' 21 Salesforce Release has introduced a new and exciting Flow feature which is Record Prior Value variable in Record Triggered Flows. This basically means that now we can see and use the Old Values (old field values) of the Record that fired the Flow and Compare old and new values of record in Record Triggered Flows. Salesforce Flow Prior Value feature can be used for many use cases and scenarios which were previously only possible using an Apex Trigger. In this post we will see the use of Prior/Old Record Value variable using an After Update Record Triggered Flow. We will implement Is Changed in After Update Record Triggered Flow.
When an Account's Shipping Country Is Changed, we need to update all related Contacts of the Account and copy the value of latest Shipping country in the Mailing Country field of Contacts. Also, we need a way to copy and store old Shipping Country value of Account on all related Contact records in a custom field named "Previous Country".
Account's Shipping Country Changed --> Update Mailing Country of all Child Contact Records with latest value of Account's Shipping Country --> Also Store old Shipping Country Value of Account in a Custom Field (Previous Country) on all Contact Records.
8. Update Element : To update the Contact Records in the Collection after Loop Finishes
Compare Old and New Value of Records in Flows
Using the Prior Value Global Variable in Record Triggered Flows we can now easily compare all old and new Field Values of the Record that fired the Flow. Before Spring' 21 it was not possible to compare Old Values with New Values in an After Update Flow. With this capability now, it is possible to do an operation similar to IS CHANGED in Flows to check if a particular field value of a Record was changed or not during any update transaction. We will implement Is Changed capability in After Update Record Triggered Flow with a simple use case and see how to use Old Value of Record in Record Triggered Flows.
Is Changed in After Update Flow Salesforce
Let's see a simple use case to understand the Prior Value usage in Flow and see how to implement Is Changed in Record Triggered Flows.
Use Case - A Multinational Product company uses Shipping Country on Account Object to store Account's base Country. Contacts have two country fields : Mailing Country and Previous Country(custom field).
When an Account's Shipping Country Is Changed, we need to update all related Contacts of the Account and copy the value of latest Shipping country in the Mailing Country field of Contacts. Also, we need a way to copy and store old Shipping Country value of Account on all related Contact records in a custom field named "Previous Country".
Account's Shipping Country Changed --> Update Mailing Country of all Child Contact Records with latest value of Account's Shipping Country --> Also Store old Shipping Country Value of Account in a Custom Field (Previous Country) on all Contact Records.
Sign Up for the Salesforce Spring '21 Pre - Release Org to Play with this and other latest Features : https://www.salesforce.com/form/signup/prerelease-spring21/
Before we move ahead : If you like reading my content, Do Subscribe to my Upcoming YOUTUBE Channel to receive latest updates here: https://www.youtube.com/channel/UCdfi8Sq7iOcxnFhsgulWHeg/videos?view=0&sort=p&flow=grid
How to check if a Field Value was Changed in a Record Triggered Flow?
Note: In Spring'21, Salesforce introduced a new Global Variable in Record Triggered Flow named Record Prior ($Record__Prior). As the name suggests, this variable is similar to the $Record variable except that it contains all the Old field values of the Record that fired the Flow and basically represents the Record's old state before the update was made on that Record.
As per our use case, we need to identify if Shipping Country was changed for Account when any update is made on Account. In a Record Triggered Flow this wasn't possible earlier. But now we can achieve this using a Global Record variable name Record Prior ($Record__Prior) in Triggered Flows. Also, using this same variable (Record Prior) we can find the old value of the country and store it in the Contact record's field "Previous Country".
Let's See this in Action!
Prior Value in a Record Triggered Flow to Compare Old and New Field Values
Let's see the Flow Design and Steps to achieve this functionality and implement our use case. Below is the Final Flow Design. By looking at this design we can guess how the implementation will look like.
Checkout this Video for Complete Steps and Flow Creation
Let's Also see the Steps and Details to create this Flow.
1. Start Element : We will choose the Record Triggered Flow of type After Update and Object Account since the event that will fire this Flow is simply when Account is Updated. No criteria is needed for this Flow in start Element as we need to fire this Flow for every Account Update.
Note: The Is Changed filter or criteria cannot be applied as of now directly in the Start Element. We will apply that later using the Decision Element in next step.
2. Decision Element : To check if Shipping Country Field was Changed on Account
Use of Record Prior Variable to compare Old and New Field values of the Record in Flow
So now we will just compare the new value and old in Shipping Field of the Account Record that Fired the Flow.
Until Spring' 21 we already had the $Record Global Variable in all Record Triggered Flows which basically is gives us all field values of the record that fires the Flow.
From Spring' 21, we will now also have a $Record__Prior Global Variable available in the Record Triggered Flow which will have all the field values of the same record that fired the Flow before it was updated.
So using these two Record Variables we will just see if the Shipping Country values in these two versions of the same Account Record is same or not.
The most Important point here is to note the usage of $Record and $Record__Prior in the criteria. Since both point to the same record, we just use one on either side and just check if old value of Account's Shipping Country was equal to new value or not. Because if they are different then it means that Shipping Country was changed. And that's all we need to know to update contacts.
3. Get Elements : To find the Contact Records on the Account that Fired the Flow.
We will Search if the Account that Fired this Flow has any related Contacts. Here we are searching for Related Contacts by using the Account's Id as the criteria. Again we will use $Record.Id to find all contacts which have this Id in their AccountId field. The name of this Get Element is {!Find_Related_Contacts} which now will behave as a Collection Variable as we are storing multiple values in this. We will use this {!Find_Related_Contacts} in later Flow Elements so remember the Variable name.
Important Note: Even if we used $Record__Prior.Id here instead of $Record.Id to find contacts for the Account that fired the Flow, it would have still worked same. Because Id field never changes in old and new version of the Records in Salesforce for sure.
We are just checking if any Contacts were found for the Account that fired this Flow using Get Element in the Last Step. The Flow will process further only if any Contact Records were Found.
To check this we will just see if the Get Element Collection variable created in last step {!Find_Related_Contacts} is Null or not. Only if it is not Null we will proceed with the Flow Process.
We'll be Looping/Iterating on Contact records (List or Collection) that were found in Get Element in Step 3 one by one.
In every Loop iteration we will have a contact from the Contact List/Collection in the Current Loop Iterator variable. We will Assign the new Mailing Country value to the current Contact (current Loop iterator) which will be equal to the new Shipping Country of Account.
Also, we will assign value to Previous Country field on contact which will be equal to the Old Shipping country of Account.
These two steps we will see in detail later.
6. A new Collection Variable : To store all Contacts that will be Updated at once using Update Element.
This is because we wont use Update Element inside the Loop. In over to avoid updating each Contact Inside the Loop, we need to store each contact to update in a collection variable and then use this collection variable in Update Element to update all Contact Records stored in it.
The Variable will be of object Contact and enabled to store multiple values.
In Step 5, we started a Loop on Contact Records (Collection Variable) found in Get Element ( Step 3).
So now, every Loop Iteration will give us a single Contact record from the Collection of Contacts one by one in the Loop Variable.
In every Loop iteration, a single Contact Record can be accessed using the Loop variable named "Current Item From Loop Loop_on_Contacts" where "Loop_on_Contacts" is the name of our Get Element.
Note: This Loop variable is created automatically in the Flow whenever we use a Loop Element and we don't need to create it manually.
Using Assignment Element inside the Loop, First we will apply field values to this single contact record which we get in the Loop Variable explained above.
For every Contact in a single Loop iteration , we will assign two values as below :
Contact.Mailing Country = $Record. Shipping Country (New Value of Account's Country)
Contact.Previous Country = $Record__Prior. Shipping Country (Old Value of Account's Country)
8. Assignment Element 2: To add this Single Contact Record in each iteration to new Collection Variable
Adding the Current Contact from Loop Iterator to the New Collection Variable of Contact that we created in Step 6. The name of the Collection variable was ContactsToUpdate.
In every Loop Iteration we will add the single Contact Record into the new variable (Collection of Contacts). As we saw in the Last Step the Single Contact record is available in the variable named "Current Item From Loop Loop_on_Contacts". So we just simply add this current contact in the collection. Check screenshot below and Video at the start for more clarity.
Just update all contacts that we added to new Collection named "ContactsToUpdate" in last step.
And we are Done!
Hope this Helps! Reach out to me here or on LinkedIn for any questions, issues or suggestions. Happy to Help!
If you like reading my content, Subscribe to my Upcoming YOUTUBE Channel here: https://www.youtube.com/channel/UCdfi8Sq7iOcxnFhsgulWHeg/videos?view=0&sort=p&flow=grid
Checkout all of the Latest Salesforce Spring'21 Features : Spring'21 Flow Preview
Don't Miss Latest Posts : CLICK HERE TO SUBSCRIBE TO THIS BLOG
Checkout Flow Examples and Tutorials : All Flow Examples
Hi,
ReplyDeleteCan you highlight "IsChanged" and "PriorValue" is available in Spring 21 from Salesforce Release Note. Because looks it only available from Summer 21. I tried deploying a flow with IsChanged from Summer 21 to Spring 21 Org, received the error [''IsChanged' is not a valid value for the enum 'FlowRecordFilterOperator']
https://help.salesforce.com/articleView?id=release-notes.rn_automate_flow_builder_record_triggered_flow_formula_functions.htm&type=5&release=232
I struggled with getting a record triggered flow to work using record prior as a decision. The only way it ultimately worked out for me was to start with the flow with an assignment and assign the prior value to a variable. I could then use that variable in a decision. Not sure if anyone else has run into this. Hope it helps
ReplyDelete