EP - 31 | How to use Collection Filter In Flow | Lightning Flow Series ⚡

 

How to use Collection Filter In Flows

In this blog we will learn how we can use collection filter element to filter out an existing collection of records.
With the help of this element your flow will be much more easier, efficient and less complex.

What does this element actually do?

With this element we can add filters to a collection without touching the source element.

You can filter any collection found in Flow Builder, including collection variables that contain single values, collection variables that contain records, and Apex-defined collection variables.

Collection Filter outputs a collection with the filtered results and doesn’t change the contents of the source collection. The output collection is null until its corresponding Collection Filter runs.

NOTE - The output collection variable is named after the Collection Filter element's API name.

Use Case -

Our client wants to show two separate list of accounts on the same screen with one list containing all the accounts with Billing Country "India" and the other one containing accounts with Billing Country "France".

With that he also wants to have a collection of accounts without any filter which he might use in the future.

Now, in this case if we do not have Collection Filter Element then what will be the solution?

  1. Take three get records element. One containing all the Accounts. Second one will contain Accounts with Billing Country "India" and Third one will contain Accounts with Billing Country "France". But hold on and think, is it the right way? No! It's Not. 👎 Using three queries on the same object just with different conditions is not correct. Obviously!! 🤷‍♀️
  2. Take one Get Record Element in which get all the Accounts without any condition. After that take a loop element and loop through all the accounts, add a decision element to check whether the country is "India" or "France" and add the accounts to a collection variable accordingly.
But that's too complex right? 🤷‍♀️
Here comes Collection Filter Element. 

Here is what we have to do -

1. Take a Get Record Element and get all the accounts without any condition.



2.Take a Collection Filter Element and in the section "Filter Items in this collection" for Collection option choose your "Get Record Element".



3. In Apply filter condition we will apply "Billing Country" equals to "India"
4. Click Done and that's it.

Now, you have a collection of records with billing country "India" and guess what your Get Record Element is still the same. It has all the accounts from your org.

After that repreat step 2 to 4 but just change the condition value in step 3 and add "France".
Now, you have another collection of records with Billing Country "France".



There you have it. 
  • You have a Collection of All the Accounts.
  • You have a Collection of Accounts with billing country "India".
  • You have a Collection of Accounts with billing country "France".
Now, Let's take a screen element and show two Data Tables in which we will add our collection filter elements and show result for "India" and "France".




Here is how the flow looks :



Did you notice? We did all of it from just getting the records once from the database. That's what collection filter is all about.
It filters and gives you a new set of records without changing the source collection.

Checkout the output :


To learn this example with video tutorial, please visit the below YouTube video :



If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Let's learn and grow together.
Love and Peace! 

Post a Comment

0 Comments