Flow Bulkification | Mass Update Records from Flows in Salesforce

Update Multiple Records in Flow Salesforce. In this post we will talk about ways to Bulkify Flows to Update Multiple Records .This is also known as Flow Bulk Update or Bulk Update / Mass Update of Records using Flows. Concept of Salesforce Flow Update using Record Collection Variable will be explored in this post. We will also see how to use Salesforce Flow Loop Update Records feature. Also, we'll talk about some Flow Loops Best Practises. 

Let's see How to Use Loops to Update Multiple Records in a Flow.

Flow Bulkification | Mass Update Records from a Flow in Salesforce

How to Update Multiple Records in Salesforce Flow?

Flow has an Update Records Element which can be used to Update Records for any object. Updating a single Record is straight forward and there is not much to worry about in terms of best practices. 

The tricky part is UpdatingMultiple Records for a particular object at once. At least, it proved a bit tricky for me when I tried this for the first time. 

There were two ways I thought of doing it initially
1. Using Update Record Element multiple times in a Flow. Either inside a Loop or otherwise.
2. Using a single Update Record Element and pass all the records to be updated and create them at once. Obviously, second way is the correct way!

Important: Make sure the number of Records in the Flow are not violating the Flow Records and element limits .

NOTE : This article is showing Flow Bulkification while Updating Multiple Records in a Salesforce Flow. 

If you want to see how to Create Multiple in Flows or Mass Create Records in a Flow then checkout this Article for better understanding : Create Multiple Records using a Flow

Never Use Update Records Element inside a Flow Loop. Never!

Please remember this point always. Because, an easy way to Update multiple records is to use the Update Records Element inside a loop. But don't do it.

Because of Salesforce Governor Limits : If we are dealing with a large volume of records, there's a good chance we will hit the limits. 

How to Avoid using Update Records Element inside Loops in Flow?

The best way to avoid using Update Record Element inside a Loop is to actually create a New Collection Variable of type Record to hold/store all the records to be updated and use a single Update Record Element at last for this Record Collection Variable.


Learn Salesforce Flows
Become Flow Expert - Click Image and Start Learning

Salesforce Flow Update Multiple Records

When we Loop on Collection Variables to Update Records, we need to make sure we assign every Loop iteration variable to a new Collection Variable which should be used at last to Bulk Update Records of the Collection Variable on which Loop was running.

The Trick : Basically, we need to create a New Collection Variable (type:Record)  for every collection variable used in Loop when we need to mass update records using Flow Loops.

How to Use Loops in a Flow to Update Multiple Records?

Let's see this in more detail using simple Scenario.

Note: I am using this old Article to show this concept here. You can directly check that out here Update Multiple Records from Salesforce Flow  
Otherwise, you can continue on this article below.


Sample Scenario : Update all Child Contacts of an Account when Account is Updated

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. Alsowe 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.

I am using Spring '21 feature of Prior Record Value. So signup for a preview org to implement this. 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/
Flow Bulkification | Mass Update Records from a Flow in Salesforce

We will use an After Update Record Triggered Flow on Account Object

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.


Flow Bulkification | Mass Update Records from a Flow in Salesforce

Mass Update Records from a Flow in Salesforce

2. Decision Element : To check if Shipping Country Field was Changed on Account

Spring' 21 : We will 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.

Flow Bulkification | Mass Update Records from a Flow in Salesforce

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.

Update Records from a Flow in Salesforce
Bulkification | Mass Update Records from a Flow in Salesforce


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.
Flow Bulkification | Mass Update Records from a Flow in Salesforce
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.

Flow Loops to Mass Update Records from a Flow in Salesforce

4. Decision Element : To check if any Contacts were found for the Account

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.
Flow Loops to Mass Update Records from a Flow in Salesforce


5. Loop Element : To Loop on Contacts (If they were Found)

We'll be Looping/Iterating on Contact records (List or Collection) that were found in Get Element in Step 3 one by one. 

Flow Loops to Mass Update Records from a Flow in Salesforce

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.  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 new 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.

Use FlowLoops to Mass Update Records from a Flow in Salesforce

7. Assignment Element 1: To assign field values to contact in Loop one by one

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)

Flow to Mass Update Records from a Flow in Salesforce
Update Records from a Flow in Salesforce
Update Records from a Flow in Salesforce

So finally this Assignment Element will look like as below:

Update Records from Flow Loops in Salesforce

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.

Update Records from Flow Loops in Salesforce
8.  Update Element : To update the Contact Records in the Collection after Loop Finishes

Just update all contacts that we added to new Collection named "ContactsToUpdate" in last step.

Update Records from Flow Loops in Salesforce
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

Don't Miss Latest Posts :  CLICK HERE TO SUBSCRIBE TO THIS BLOG

All Flow Examples and Tutorials on this Blog : All Flow Examples

Learn Salesforce Development from Scratch


Comments

  1. Hi Vibhor,

    I'm currently trying to follow along and running into an issue when trying to call the prior Account value ($Record__Prior). I only have access to $Record. I'm using a developer edition, triggered flow, after save and only when Account is updated. I can't see $Record__Prior for some reason.




    ReplyDelete
    Replies
    1. Hi, Sorry I forgot to add the preview org link here as this is a spring'21 latest feature ..signup here https://www.salesforce.com/form/signup/prerelease-spring21/

      Delete
  2. Thanks so much just signed up and am able to see it now.

    ReplyDelete
  3. Hi Vibhor, in a scenario where you only have one assignment element, how do you close the loop while keeping the update element outside the loop?

    ReplyDelete
    Replies
    1. if we have only one assignment, then how will you assign the single records to collection variable? I think we need two as mentioned. one to assign values and one to add in collection ... let me know if you have other thoughts

      Delete
    2. Thanks that makes perfect sense.

      Delete
  4. Maybe a silly question, but why do you have to add the contacts to update to a new collection variable? Why can’t you simple update the original collection variable?

    ReplyDelete
    Replies
    1. I think because it disappears once your outside of the loop

      Delete
  5. I am having an issue with hitting apex process timer limits with the update. I am only updating a 160 records in the collection outside of the loop. almost the exact same use case. thoughts?

    ReplyDelete
  6. Shakira Xindao13 May 2022 at 13:11

    Thanks for this post. Should this also work with a schedule Trigger flow? ('Cause I get Error Too many future calls)

    ReplyDelete
  7. This doesn't work if there are more than 2000 iterations.

    ReplyDelete
  8. Hi,
    I am trying to update a field by providing value in another field(if I provide any value in the field other picklist field value should be updated from one stage to another stage) in multiple sections. How Can I do it?

    ReplyDelete
  9. As per my understanding,
    Collecting all the data to update in collection variable and DMLing at once is 'bunching', this is different from 'Flow Bulkification', which Salesforce does automatically.
    References:
    https://help.salesforce.com/s/articleView?id=sf.flow_prep_bestpractices.htm&type=5
    https://help.salesforce.com/s/articleView?id=sf.flow_concepts_bulkification.htm&type=5

    ReplyDelete

Post a Comment

Thanks for your Feedback!

PLATFORM APP BUILDER CERTIFICATION - 78% OFF

POPULAR POSTS

Salesforce Flow Examples : Flow Use Cases and Scenarios

Flows | Use Flows to Bulk Update Records from List View in Salesforce


Never Miss Latest Posts