How to Use the Collection Filter Element

How to Use the Collection Filter Element

Salesforce introduced a new flow element called Collection Filter in the Spring ’22 release. It is available in all the core flow types. Although it is not one of the main elements that you need in every flow, it is a very important one indeed. When it comes to performance, it can be even life saver for flows.

As you can guess from its name, Collection Filter element filters collections. It lets you filter down an existing collection into a smaller collection using some criteria or a formula. Let’s say that you have a record collection that contains many records and you want another collection of records, which are already included in the first collection. Instead of using the Get Records element, you can use the Collection Filter element to filter it down to a new collection.

Configuration of the Collection Filter Element

1- Before using this element, make sure that you have a collection in your flow. Then add the Collection Filter element to the canvas, give it a name and select an existing collection.

New Collection Filter element to filter an opportunity collection

2- Enter a criteria or a formula to filter the collection. If you want to enter a formula, you can use the formula builder and check the syntax. In that case, use {!currentItemFromSourceCollection} to build your formula condition.

Build a formula to filter the collection

It is possible to use this element with collections of other data types (text, number, date, etc.) too. In those cases, you have to use {!currentItem_ApiNameoftheCollectionFilterElement} in your formula criteria.

Filter collections of other data types

3- After adding the filter criteria or formula, click Done. This will not override the selected collection, instead, it will create a new filtered collection. Therefore, you can use this element again and again to filter the same collection with a different criteria.

New filtered collection

Performance and Limit Usage

As you can understand from the explanation, this element is creating a collection according to the criteria that you define. So, it is more or less like the Get Records element. So why should you use it?

Here comes the most important part: Collection Filter element doesn’t consume from SOQL limits! You don’t need to perform another get records to have a smaller collection. At the other hand, you don’t don’t need to loop through the items in your collection and use a decision element in order to create a smaller collection. Therefore, thanks to this element, you will less likely hit the governor limits.

Examples

1- Let’s say that you want to calculate the email open rate of a subscriber. In order to calculate the rate, you have to get all the email messages, then get the ones that were opened, and then calculate the rate.

Instead of adding two Get Records elements, you can get all the email messages related to the subscriber, then use a Collection Filter element to filter the ones that were opened.

Calculate email open rate
Get opened email messages

Read this post to see the rest of this example and learn how to calculate the email open rate.

2- Let’s say that you have a collection of accounts and you want to find the one that has most open cases. It means that you have to get the open cases for each account. It sounds like the solution is to use the Get Records element in a loop. However, it is not recommended to perform SOQL or DML in a loop because of the governor limits.

In this case, you should use the Get Records element to bring all the open cases. Then use the Collection Filter element in the loop to filter the related cases of the current account record.

Collection Filter in a Loop
Filter related cases of the current account record

After this element, you will have a filtered case record collection of the current account record. So that you can continue with the decision and assignment elements.

At the end, the whole flow will consume only two SOQL limits instead of account count + 1.

To sum up, it is a great element that improves the performance of the flows. Whenever it is relevant, you should use the Collection Filter element instead of the Get Records element.

Be the first to comment

Leave a Reply

Your email address will not be published.


*