Salesforce Flow: How to Remove Duplicates from a Collection Variable. For Real.

Here’s the thing: if you search the interwebs for how to remove duplicates from a Salesforce flow collection variable you’ll find all kinds of blog posts promising to show you — but then they don’t. Instead, they tell you how to remove common items found between two different collections. Boo on them!

How to Dedupe a Single Collection Variable in Salesforce Flow

So, if you’re here to learn how to remove duplicates from a single collection variable, you’re in the right place. Here’s what it looks like from above:

As you can see, there’s more going on here than you might expect. Also, you may have noticed this is a screen flow but this can be done in any flow type. And once you remove the screens (which are not needed), the deduping process involves 4 flow elements and 2 resources.

What you don’t see above is the first part of the flow. That part creates the list that I want to dedupe. In this case, it’s a list of the BillingState field values from every account in this particular dev org.

Finally, this visual is also a good reminder of the loops we have to go through to get a job done in Flow Builder that can be done in as little as one line of code in Apex. (More on that at the end.)

Let’s get started!

Step 1: Sort the List

To remove duplicates, you must sort your list (you’ll see why later). For text fields, like BillingState, this means alphabetically. So, once you have the collection variable you want to dedupe, add a Collection Sort element to your flow. Here’s what mine looks like on the inside:

Honestly, the sort order (ascending or descending) doesn’t matter. What matters is that the collection is sorted so that duplicate items are next to one another in the list. Here’s what my collection looks like after being sorted:

Notice we have two duplicate items (CA & OR). Also, notice that we have garbage data (Singapore & UK) — that’s just Salesforce keeping it real for us in their dev org (but cleaning up those entries is beyond the scope of this post).

Step 2: Create an Empty Collection

For this dedupe method, we’re going to create a new resource (a collection variable) that will hold our final list of non-repeating items. In my flow, my original collection variable is called AccountBillingStates. My new collection is called FinalCollection.

Make sure the Allow multiple values (collection) box is ticked and that the Data Type field matches the type of the original collection.

Step 3: Create a Loop

Drop in a Loop element onto your flow canvas and choose the collection you want to dedupe as the target collection. (Refer back to the first image to see where these elements are placed.)

Step 4: Create a Variable to Hold the Last Item

Go to the Toolbox tab and create a new resource. For the type, select variable and call it LastItem since it will be always be assigned the last item added to the FinalCollection variable. Make sure the data type matches the type of the collection variables (eg, text). Starting off, LastItem will be equal to null (and that’s okay!).

As you see above, just keep the Default Value blank. And as a reminder, this variable only holds one item.

Step 5: Add a Decision Element

Next, drop a Decision element onto your canvas. This is where we check to see if the current item in the loop (taken from the starting collection) matches the last item placed in FinalCollection. If there’s a match, we’ve found a duplicate.

Important: The very first item in the loop will always be compared to null. That’s because there are no items in FinalCollection yet and our starting value for LastItem is null. This means the first item in the loop will always be added to FinalCollection (kicking off our dedupe process).

When a duplicate is found — meaning the current item in the loop equals LastItem — the current item is ignored and not added to FinalCollection. (Review the first image above to see the Decision element branches.) This is why the collection needs to be sorted. Specifically, we’re not comparing the current loop item to all the items in FinalCollection. Instead, we’re comparing it to LastItem (the item most recently added to FinalCollection).

Step 6: Add an Assignment Element

When no duplicate is found, two things happen: the current item in the loop is (1) added to FinalCollection, and (2) assigned to the variable LastItem. This is all done in the same Assignment element:

This process then repeats itself until all items of the original (starting) collection have been looped over. In the end, FinalCollection will contain all the items of the starting collection without any duplicates. Here’s what this looks like in our screen flow:

Notice that we no longer have duplicate CA or OR entries.

How to Dedupe a List in Apex

In Apex, removing duplicates is a lot easier since no looping is involved. Instead, we take advantage of the different types of collections, which include lists, sets, and maps. Here we’re just concerned with lists and sets.

By default, sets do not accept (or hold) duplicates. Therefore, we can simply assign our list to a set to remove them. (And we don’t even need to sort the list first!) It would look like this:

//unordered list with duplicate entries (CA & OR)
List<String> billingStateList = new List<String>{'CA', 'AZ', 'CA', 'OR', 'IL', 'KS', 'NY', 'NC', 'OR', 'TX'};
System.debug('billingStateList: ' + billingStateList);

//convert list to set (this removes duplicates)
Set<String> billingStateSet = new Set<String>(billingStateList);
System.debug('billingStateSet: '  + billingStateSet);

//convert set to list (converts set back to list if needed)
billingStateList = new List<String>(billingStateSet);

//sort alphabetically (converting set to list results in unordered list)
billingStateList.sort();
System.debug('deduped & sorted billingStateList: ' + billingStateList);

Above, it’s only line 6 that’s doing all the heavy lifting as we convert the list to a set. And if you want to convert it back to a list (eg, to sort it alphabetically), then you just need to add line 10. So, at a minimum, you just need one line of code — at most, two. Easy peasy!

For this reason, some admin/developers use an Apex Action element to have their flow call an invocable Apex method that does all the work. Fun! (And if you’re not up for coding that, you can find a Dedupe Record Collection flow action download on Unofficial SF.)

Was this helpful? Do you know a better, faster way to dedupe a list in Flow Builder? If so, leave a comment and let me know!

2 thoughts on “Salesforce Flow: How to Remove Duplicates from a Collection Variable. For Real.”

  1. Amazing post, really great described and such a cool solution to de-duplicate a single collection variable! Just incorporated this in my flow and it works wonderful! Thank you so much, you really made (and saved) my day!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.