Flows | Use Loops to De-Duplicate collection variables in Flow

In this article, I am going to show how to remove duplicates or common values from collection variables in Salesforce Flows using Loops.

Main Takeaways from this article:

1. What are Loops in Salesforce Flows - A basic Idea

2. Removing Duplicate values from collection variables in a Flow using Loops.

In my previous post, I had shown one approach to de-duplicate collection variables in a Flow. Please checkout that post here: De-Duplicate Collection Variables in Flows: Approach 1

Important/Caution: In this article I am just showing de-duplicate capability using "Remove All" and "Add" operators in an Assignment Element and using Loops. Loops in Flows in general have many limitations in terms of iterations and number of records that can be processed in a Flow Transaction.While designing or working with loops please consider those. 
Checkout Flows documentation from Salesforce for better understanding. 

Please Note: In order to try and showcase a Specific Use Case/Functionality, I may have overlooked some of the best practises related to Flow Development in this article. Please make sure to follow these in real world scenarios and projects. Check some really important ones below

Using Loops to De-Duplicate collection variables in Flows

In this post we will de-duplicate collection variables using Loop Element in Flows. In this article I am not going more into the details of how we fetch the data and only focussing on how to remove duplicates from collection variables.

Simple & Hypothetical Scenario (Powered by Avengers!)

Consider we have a custom object named "Avenger" in our Salesforce Org. (Yes, a Marvel fan). This object has some custom fields including Name and Superpower.

In our Flow, we used a Get Element to Query on this object using criteria "Superpower" contains Strength.

Well it returned "Hulk","Thor","Captain America"
Collection Variable 1st from Get Element 1 = "Hulk","Thor","Captain America"

We used another Get Element to Query based on criteria "Superpower" contains "Fly".
It returned "Iron Man", "Thor","Captain Marvel" 

Collection Variable 2nd from Get Element 2 = "Iron Man","Thor","Captain Marvel" 

So,Thor (who is obviously my favourite Avenger) is common in both the Collection Variables.

Requirement & Problem
Suppose we are going to update some field values for these records (from two Get elements) using one Update Element in Flow. But if we try to just update them after merging these two variables by adding one into another, it causes an issue because one Record "Thor" is a duplicate.Salesforce doesn't allows duplicate records/Ids in update/delete operations. So we have to combine them but remove the duplicates from the final list before updating them.

Use Loops in Flows to de-duplicate Collection Variables:

Here is the final design of the Sample Flow.

Step 1: Using Assignment Element: Add all the values in a Single Variable

We will first combine all the records in both the collection variables. So we will use an Assignment Element to add all the values of Get Element 2 to Get Element 1.

So Now,Get Element 1({!Avengers_1}) = Get 1+ Get 2 = "Hulk","Thor","Captain America","Iron Man","Thor","Captain Marvel" 

Step 2: Create a new and blank Collection Variable and Assign value to it

Now, we will create a brand new variable called "AvengersFinalCollection" of Object Avenger and enable it to hold multiple values. 

We will now set this variable value equal to the latest value of the combined variable (Get Element 1({!Avengers_1})) using the Assignment Element. We will add this in the same assignment element which we used in last step.

So our final Assignment Element will look like below:

So after this, the AvengersFinalCollection will have all the Avenger Records.

Avengers Final Collection ({!AvengersFinalCollection) = "Hulk","Thor","Captain America","Iron Man","Thor","Captain Marvel"

Please Note: This new variable AvengersFinalCollection will not be used for Looping.Instead, this will be used for getting the final de-duplicated list/collection of Records. We will use the combined values of both Get elements in variable of Get 1 to loop instead.

Step 3: Loop on the combined Variable with Loop variable of type Record

Remember, we added all Avengers in single variable in last step.

Since, we have added all the records in a single variable (Get Element 1 or Avenger1) in the step 1, we will just use that variable for Loop Element. We will loop over Get Element 1 records ("Hulk","Thor","Captain America","Iron Man","Thor","Captain Marvel"). 

Important: Here the Loop variable is of type Record and object is Avenger. So basically, each iteration will be an Avenger Record itself.

Note: Post Summer'20 release we need not create a Loop Variable when we Loop over Collection variables .It is created automatically . But in older orgs we will have to create a loop variable of type Record and object as Avenger.

My current org is not Summer'20 so I had to create a loop variable of type Record and object Avenger.

Step 4: Use Assignment Variable to Remove Duplicate Values 

We will loop over Get Element 1 records ("Hulk","Thor","Captain America","Iron Man","Thor","Captain Marvel"). Now, for each loop iteration, we will have one Avenger record in the LoopVariable. Basically, each iteration itself is an Avenger record. For every Loop iteration, we will use Assignment Element to perform below actions.

The trick is simple. 

During the loop iteration, whichever record is the current record (LoopVariable) we will first remove that record from the Avengers Final Collection. This is done by using "Remove All" operator in the Assignment Element.

Basically, "Remove All" operator is used to delete/remove all values(which we provide on right side of the operator) from any collection variable.

Here we are removing  all the instances or occurrences of the current Record value (LoopVariable) from AvengersFinalCollection variable.

After this we will add back the current Record (LoopVariable) to the Avengers Final Collection variable. 

Example of Loop Working Model: We are looping on ("Hulk","Thor","Captain America","Iron Man","Thor","Captain Marvel"). Let's see how this works in background. 

Iteration 1: Hulk
For first, iteration we have Hulk.In Assignment element, we will remove all the Hulk records from the Avengers Final Collection variable. Since Hulk is only present once in the list it will be removed.
Then, in second assignment, the current record, i.e. Hulk will be added back to the Avengers Final Collection. So at last this variable will have one Hulk.

Iteration 2: Thor
In the second iteration, we have "Thor" record. So since Thor is present twice in Avengers Final Collection list, both of them will be removed from it. This is because we are using Remove All

Now, as you can now see, in next assignment we will add the current loop Record, i.e. Thor back to the Avengers Final Collection. But this time obviously we are adding only one Thor record.This way we will finally achieve de-duplication since now the final list will have only one Thor record instead of two.

Similar things will happen for remaining Records/iterations. 
So, finally after Loop ends, the collection variable Avengers Final Collection will have only unique Avengers.

This is some Magic that even Doctor Strange will be Proud of!

Checkout all my Flow Posts here: All Flow Posts

If you like this please do Share! #Learn and Share!

Enter your email address:

Delivered by FeedBurner


  1. This is great. Quick Question - how does it match the records? Is it just on name? Can other fields be used to find the matches?

  2. https://salesforce.stackexchange.com/questions/359036/how-can-i-remove-duplicate-records-in-the-flow-of-a-collection-variable

  3. I read your blog on daily basis. This is really great and informative post. Thanks for sharing.

    Summer Collection


Post a Comment

Thanks for your Feedback!



Salesforce Flow Examples : Flow Use Cases and Scenarios

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

Flow Bulkification | Mass Update Records from Flows in Salesforce

Never Miss Latest Posts