Collection Filter and Collection Sort Challenge

Hello folks,
How powerful are the collection sort and collection filter elements? How should you use them effectively to optimize your flows?
I recently experimented with these elements to push their limits and see how far I could go while using only one get element. My goal was to limit the number of SOQL queries and stay within Salesforce’s governor limits.
Setting Up the Experiment
I started by creating a Get Records element that fetched all cases associated with both an account and a contact. With just this single query, I was able to extract the following insights:
-
Most Recently Modified Case
-
Oldest Modified Case
-
Oldest Modified Case Account Name
-
Number of Cases by this Account
-
Most Recently Closed Date for this Account
-
Contact ID on this Case
-
Number of Cases for this Contact
-
Oldest Closed Date for this Contact on this Case
Key Findings
How Many Elements Did I Use? Did I Have to Loop?
Here’s what I found:
-
Sorting is built into the Get Records element – Use it to order records by modified date, closed date, or any relevant field.
-
Looping is necessary for min/max values – To find the oldest and most recent records, you need to loop. However, you only need one iteration—once you assign the first record to a variable, you can exit the loop.
-
Utilize existing sort order in the loop – Instead of iterating through the entire collection, leverage sorting to pull only the first record and move forward.
-
Avoid unnecessary counter variables – Initially, I used a counter variable to track loop iterations, but this was redundant. Exiting after the first iteration is sufficient.
-
Use an assignment element for counting records – Instead of looping, apply the equals count operator within an assignment element to determine the number of cases per account or contact.
Challenges and Limitations
I experimented with formula criteria inside the Collection Filter element, but I ran into limitations. It seems that this feature does not support filters where the right-hand side is a variable. For example, this formula did not work:
ContactId on the current record = ContactIdVariable
Can Collection Sort and Filter Save You Lengthy Loops?
Absolutely! These elements help reduce unnecessary looping and improve flow efficiency. Instead of iterating through every record in a collection, you can filter and sort strategically to extract meaningful data with minimal processing.
How Many Elements Did I Use?
Excluding the Start and End elements, I only needed a handful of elements to extract all this information effectively. The combination of sorting, filtering, and looping only once significantly streamlined the process.
Join the Discussion
Join the discussion on LinkedIn here or Twitter here.
Live session recording links:
Watch Melody Lwo of Salesforce Flowsome and me either on LinkedIn here or YouTube here.
Enjoy.
Explore related content:
The Answer for the Collection Filter & Collection Sort Challenge

3 Comments