top of page

Workaround for Query only first N records in Flows – Part 1

Nov 26, 2019
2 min read

Hola! 🙂

In this article, we shall see how we can set up an equivalent for LIMIT N clause(“Select… LIMIT 5 “; SOQL, ring a bell?) in a flow.

For those who are not aware about the LIMIT N clause, what it does is sets the maximum number of records that will be returned by the SOQL. Right now, we don’t have the ability to set a maximum limit on the records returned when we are using Get records element.

But we can certainly work our way around it.

Logic:

The idea is to first do a normal query that gets all the records and then loop over the collection and add the first N records to a new collection by checking the counter variable’s value in the loop over each iteration.

It’s time to Flow!

Step 1: The usual drill, we start the flow by creating some variables.

Step 2: Use a Get Records element to query all the Account records.

Step 3: Use a Loop element to iterate over all the Account records.

Step 4: Use a decision element to keep checking the value of varRecordLimit variable inside the loop.

Step 5: If value of varRecordLimit is greater than 1, then using an Assignment element, we add the Account record to a record collection variable and decrement the value of varRecordLimit variable.

Step 6: If value of varRecordLimit is less than 1, we skip the iteration and move to the next one.

Step 7: Finally a Screen element to display the collection variable records.

Final Flow

Important Note:

This workaround is not going to work for objects which have more than 50000 records. It will rather give you an exception. Tip: Always use filters to narrow down the sized of query results.

In my next post, we shall try to address this issue when there are more than 50000 records.

Thank you for being an awesome reader! Subscribe to this blog for receiving all the latest updates straight to your inbox. 🙂

 
 
 

Comments


bottom of page