In more advanced Flows you will often need to work with collections of records. In this blog I will list some tips to make use of some of the more advanced possibilities of Flow builder related to Collections and Loops.
1 Collection Filter
The Collection Filter element allows you to isolate a subgroup of records from an existing Collection bbased on criteria and filter logic, like you can in a Get Records element. The major advantage is that you do not need to perform an extra query.
Suppose your org has a custom object called Fruit. The records have a Name and a Color. First you simply get all Fruit records. When is a screen of the flow you want to let your user pick from only the orange Fruits, you do not need to use another Get Records element. You know these are all in your original Get all Fruits collection. So you apply a filter to this collection using the Collection Filter element. This will output a new record collection containing only the orange Fruits.
2 Nested Loops with Collection Filters
A filtered Collection can also make a nested loop work efficiëntly. By a nested loop I mean a loop within a loop. Without the use of filters the number of iterations will grow exponentially. If the collection in the outer loop consists of 100 records and the collection for the inner loop does too, then without filtering there would be 100 x 100 = 10,000 possible combinations to evaluate and usually you do not really need to evaluate all possible combinations. A collection filters placed within the outer loop but before the inner loop can narrow down the number of combinations that the inner loop wil ieterate over. To demonstrate this, I will use the following business case:
Our customer sells e-Bikes. For different models, different accessories are added to purchases for free. The company uses a flow to link Products to the Opportunity. Insiode the flow employees select Products to sell and the related accessories should be added automatically.
We then use a nested loop. In the outer loop, you evaluate each manually selected bike. In the inner loop, you don't want to iterate through all accessories every time, but only those accessories that belong to the respective bicycle. For that purpose, you place a filter between the first and second loop element and let the inner loop use the filtered collection.
3 Equals Count
If you want to know how many elements (singular variables) are present in your Collection, you can certainly perform a loop over the entire collection and increment a numerical variable by 1 in each iteration, but that is not efficient. Instead, you can use the Equals Count operator in conjunction with a Collection Variable in an assignment. Equals Count represents the number of elements in the collection.
4 Collection sort
With a Collection Sort element, you can sort an existing collection in a different order. In the case of a collection with records, you can also use a different field to sort by. Suppose your original collection "Fruits" is sorted by Name from A to Z, but now you want to sort them from heavy to light. In that case, you would use a Collection Sort element to adjust the order.
Another important capability of the Collection Sort element is that it allows you to reduce the number of elements in the collection. For example, I can choose to only have the heaviest 5 fruits remain in the collection.
Be aware: While a Collection Filter element introduces a new variable for its output and leaves the original collection untouched, a Collection Sort element does actually modigy the original collection. So if u use a Collection Sort to limit the number of records, those that you remove are really gone.
5 Operators
Different types of variables have different capabilities when it comes to assigning values. With collections, just like with text and numbers, it is possible to:
- assign a new value using Equals
- This replaces the entire value and in the case of a collection, all the values that were in the collection before.
- add an extra element to the collection using Add
- The elements that were already in the collection stay in there and an extra element is added
Apart from these, there are a few other interesting possibilities for Collections.
- Remove After First: removes all elements after the first occurrence of the value you specify for comparison.
- List before this assignment: Red, Yellow, Green, Blue
- Remove after: Yellow
- List after this assignment: Red, Yellow
- Add at Start: inserts an element at the first position of the collection, causing all existing elements to shift one position.
- List before this assignment: Red, Yellow, Green, Blue
- Add at Start: Purple
- List after this assignment: Purple, Red, Yellow, Green, Blue
- Remove All: removes every element that is equal to the value you specify for comparison.
- List before this assignment: Red, Yellow, Green, Yellow, Blue, Yellow
- Remove All: Yellow
- List after this assignment: Red, Green, Blue
- Remove First: removes only the first element that is equal to the value you specify for comparison. Every subsequent occurrence of an element with that value will remain in the collection.
- List before this assignment: Red, Yellow, Green, Yellow, Blue, Yellow
- Remove First: Geel
- Lijst na Assignment: Rood, Groen, Geel, Blauw, Geel
- Remove Before First: verwijdert alle elementen die voorkomen vóór de eerste keer dat de waarde voorkomt die je voor de vergelijking aangeeft.
- List before this assignment: Red, Yellow, Green, Yellow, Blue, Yellow
- Remove Before First: Yellow
- List after this assignment: Yellow, Green, Yellow, Blue, Yellow
- Remove Position: removes the element at the position indicated by a numerical value that you provide.
- List before this assignment: Red, Yellow, Green, Yellow, Blue, Yellow
- Remove Position: 2
- List after this assignment: Red, Yellow, Yellow, Blue, Yellow
- Here Red is position 0 in the original collection, so position 2 is Green
- Remove Uncommon: removes all elements whose value is not equal to the value you specify for comparison.
- List before this assignment: Red, Yellow, Green, Yellow, Blue, Yellow
- Remove Uncommon: Yellow
- List after this assignment: Yellow, Yellow, Yellow
On this page on the Salesforce Help siteyou can see examples of what each operator does to the collection.
If you want to ensure that the rest of the collection is not evaluated after one of the iterations of the loop, you can place an assignment after the correct path of a decision element where you empty the collection on which the loop is iterating.
A safer and more predictable solution is the following.
Often (but not always) you can actually solve these types of scenarios more effectively using a collection filter. This way, you ensure that only the correct records are evaluated even before entering the loop.