How to Use the Collection Sort Element

How to Use the Collection Sort Element

Salesforce introduced the Collection Sort element back in the Summer '21 release. It is possible to use this element to reorder the items within a collection by multiple fields. Moreover, it is possible to limit the collection after sorting. Here is how and when to use this element.

How to Use the Collection Sort Element

Add this element and select any collection variable that you want. Then, choose the fields that you want to sort by. If the collection variable has more than one field, you can click Add Sort Option to sort by multiple fields. The order of the fields matters in this case. Therefore, make sure that you add the fields in order of greater to lesser priority. You can sort by up to 3 fields at a time.

Collection Sort Element

If you mark "Put empty string and null values first", it puts the records with an empty or null value in the Sort By field at the start of the collection. Otherwise, they will be at the end.

The second purpose of this element is about limiting the collection. It is possible to keep all items or limit the collection. In order to limit the collection, choose "Set the maximum number of items" and enter a number. Pay attention that this is the maximum number of items. For instance, if your collection has 5 items and you want to keep 10 items, it won't fail. It will keep those 5 items.

Limit Collection Using Collection Sort Element

Importance of the Element

While Collection Sort isn't used much compared to other tools in flows, it can be really handy in specific cases. Although it is possible to perform sorting directly within the Get Records element, it is not possible to sort by multiple fields. On the other hand, if the collection is not created via Get Records element, you cannot use the Get Records element to sort the collection. Therefore, you have to use the Collection Sort element in order to sort the items.

In SOQL, LIMIT is a very crucial clause that is used in many use cases. Even though it is not possible to limit a collection directly from the Get Records element, you can do it using the Collection Sort element. This makes it a very useful element in Salesforce Flow.

Best Practices

Unlike the Collection Filter element, Collection Sort element makes the changes directly in the selected collection variable. It means that this element overrides the selected collection variable. Therefore, it is important to place this element in the correct position within the flow. For instance, if you limit a collection and choose to keep 10 items, the collection itself will have 10 items and the rest will be removed. If you need to check the size of the initial collection, you must do it before the Collection Sort element.

This element works with collections and the output is always a collection. This means that if you limit the collection to 1 item, it will still be a collection that has 1 item. Therefore, you will need to use Loop and Assignment elements to assign that item to a single variable.

Example

Let's build a screen flow that displays a data table of top 10 closed won opportunity records and the number of closed won opportunity records.

1- Add a Get Records element to find the closed won opportunity records.

Get Opportunity Records

2- Add an Assignment element to assign the size of the collection to a number variable. It is important to do it before the Collection Sort element, otherwise you cannot find the correct record count.

Assignment Element for the Record Count

3- Add a Collection Sort element to sort the collection and limit it to 10 items.

Collection Sort Element to Limit Collection

4- Add a Screen element with a data table to display the records.

Data Table

Then, add a display text to display the number of the opportunities.

Display Text Component

At the end, your flow should look like this.

End of the Flow

Here is the result of the flow. As you can see, there are 14 closed won opportunity records and the data table is displaying the top 10.

Result of the Flow

Here is what happens if you put the Collection Sort element before the Assignment element. As you can see, the data table displays the top 10 records. However, it doesn't display the correct number of closed won opportunity records. It is because the Collection Sort element overrides the collection variable.

Data Table with Wrong Order of Elements

Read this post to see a little bit more complicated example.

4 Comments

  1. I had just discovered the limit feature in collection sort that gave me a great idea for a solution i was working with.
    I needed to label feed messages so that the latest one gets stamped "Latest" and the one right before that gets stamped "Previous" (working with a formula to concatenate wtvr the body of the feed item is + the word "Latest")

    There are probably other ways to do this, but to identify the 2nd latest feed item i did 1.a get records of all feed items of that record
    2. i then sorted them by created date DESCENDING, limit 2 records (which gave me the very latest record, as well as the second latest record)
    3. then another collection sort sorted by created date ASCENDING (which left me with the 2nd latest record 🙂 )

    its a little hacky so let me know if theres a better way to do it!

    • Hi,
      It sounds good but I think you can loop through the first collection (with 2 items) and assign them to 2 different variables using assignment and decision element in the loop. Assign the first one, then if the first variable is not null (it means you have one more record), then assign the second one to another variable.

  2. Yumi if im using collection sort in order to obtain only one record from a collection is it possible to obtain it without (the id) using any loop?

Leave a Reply

Your email address will not be published.


*