Actions can be performed on multiple records by using collections in Flow Builder. Collections can be described as a list of records or values that you want to work on. If you don’t know how to use collections and loop element to iterate over those items, you can read this post: Working with Collections in Flow
One way to create a collection is choosing to store all the records in a Get Records action. Flow Builder creates a collection and stores those records in it.
As you can see from these options, you can store only the first record or all of the records that meet your criteria. However, what if you want to limit your collection and store only first N records in the collection? When you are using SOQL, this is possible by writing LIMIT 5 (5 is an example here, it can be any number) at the end of your query. There is no option for this in Get Records element in Flow Builder. However, there are ways to achieve this.
Using Loop to Limit the Collection
Loop can be used to iterate over the items in a collection of records and assign first N records to another collection.
For example, here is how to store top 5 accounts according to a custom field called Net_Sales_Last_6_Months__c
1- Get all the records that meet the criteria and sort them by this custom field in descending order.
This will bring all the accounts that meet the criteria, so there can be many records in the collection.
2- Use a loop to iterate over items in the collection. Since we sorted the collection to store accounts according to the custom field in descending order, make sure that direction of the loop is from first item to last item.
3- Create a number variable and set the default value as zero. Create another collection record variable for accounts. This new collection will store the top 5 accounts that we will extract from the existing collection. Use an assignment element to add 1 to the number variable and assign the current item from the loop to the new account collection.
4- Add a decision element before the assignment to check the record count. If “Count” variable is 5, it means that we have already assigned 5 records to the collection, so the loop can end.
5- Your flow should look like this. Note that “Check the Size of the Collection” is not necessary, you can continue with your next action.
Using the Collection Sort Element
First of all, thank you for reading until here, it is always good to learn a workaround. However, starting with the Summer ’21 release, there is a standard way to limit a collection. Here is how to do it:
1- There is a new element called “Collection Sort”. It lets you add multiple sorting to a collection, which means that you can sort records in a collection by multiple fields (up to 3). Second thing that you can do with this element is to limit a collection.
This element works with any kind of collection but if it is not a record or Apex-defined collection, it cannot be sorted by a field. Instead, it can be sorted in descending or ascending order.
To learn more about Collection Sort element, read this article.
Drag this element onto the canvas or if you’re in auto-layout, click and select Collection Sort.
2- Select the existing collection, add a sort option, and control the collection size. After sorting, the collection keeps the first items, up to specified maximum, and removes the rest.
In our example, we will sort by Net_Sales_Last_6_Months__c in descending order and limit the collection to 5 records.
3- Your flow should look like this. Again, note that “Check the Size of the Collection” is not necessary, you can continue with your next action.
Even though there is a standard way to limit a collection now, this feature is still not in production. It will be available in Summer ’21 release. After this release, you can ignore the first option. However, it is always good to know a workaround because it lets you think out of the box and bring different perspectives to meet your business needs.