Query only first N records in Flows
Hi! 🙂
I hope this will be my last post on the this topic as I’ve done quite a few posts already. 😬 Here is the list:
For this article, it’s not necessary for you to go through all of the afore-listed articles though.
Why this post then?
Well, all the solutions/workarounds mentioned in the listed articles work, however their usage is very limited from a reusability standpoint. For example, solutions discussed in article 3 and 4 are most effective among the lot as they’re Apex Actions which are generic to some extent. But for use cases where you’d want to have multiple filters, the possible permutations and combinations of SQOL query are too many to be covered by a single generic Apex action.
What’s the solution then?
Let’s first discuss the complicated one, ExecuteSOQL action. This action gives you the flexibility to execute a SOQL directly. But it has a major security concern. If you’re not aware of how to do SOQL queries that respects users’ permission then the flow might end up modifying the data the end user doesn’t have access tom because the Apex action runs in ‘without sharing’ context. So, I’d NEVER recommend using something like this. Also, you need to have good enough understanding of SQOL for writing direct queries.
And that brings us to the second alternative which is much simpler and takes care of a lot of the pain points.
Approach
So the idea is to simply combine Get Records and an Apex action which acts as an equivalent to LIMIT N clause in SOQL queries. Using the Get Records element, you’d simply query all the data and then use an Apex action to give you the first N records from the collection queries records. NOTE: This approach will certainly affect the processing time adversely, especially if the record collection is huge.
So how can you install the action?
You can find the source code and the package link here: https://github.com/forcePanda/Flow-Repo/tree/master/Apex%20Actions/Get%20N%20Records%20from%20RecordCollection
If you want something more fancy and flexible, say, get all the records after the first five records in the collection or records between two indexes of the collection, you can checkout the following action by Munawir Rahman: https://github.com/munawirrahman/InvocableCollectionSlicer
I hope you find this useful! I’ll catch you in the next one! ✌ Thank you for being an awesome reader! Subscribe to ForcePanda to receive all the latest updates straight to your inbox. 🙂

.png)




Comments