Workaround for Query only first N records in Flows – Part 3(Generic solution) #Spring20Delight

Hi there! 😀 (No, I am not gonna say ‘I am using WhatsApp’)

Let me start with jogging your memory a bit. I published a couple of posts in past month where we discussed a workaround for Query only first N records in Flows and we came to a conclusion that it’s not possible to have a generic solution for this problem as of yet.

Well, that’s going to change very soon! Yes, you heard it right. After Spring 20 release, we will be able to pass sObject(and collection) as parameters to Invocable methods. Cool, right?

Safe harbor, stuff that you are going to see in this post is not GA yet. But soon you will be able to use it once the Spring 20 is released to production environments. I am using a pre-release org for the demo purpose.

How does that help us?

Well, it changes everything *heart eyes*. We will no longer have to create separate InvocableVariables for each object and we can just use 1 single sObject collection variable as InvocableVariables to hold the records of any sObject type. So this enables us to write a generic reusable Apex Invocable method, which we will be able to use for any type of sObject collection in our Flows.

Apex again? 🙄🤦‍♂️

Dear admins, worry not. You probably will not have to deal with this Apex class much. It’s just a one time setup and you can then relax sipping martinis 🍸 on a beach.
All you have to do is just create this Apex class.

public class QueryNRecords {
    /* Class definition for throwing custom exceptions */
    public class FlowApexActionException extends Exception{}
    @InvocableMethod(label='Query N records' description='Returns a list of N records, where is N specified by a user as a flow input.')
    public static list<QueryResults> getNrecords(QueryParameters[] queryParams){
        if(queryParams[0].numberOfRecords >= 50000)
            throw new FlowApexActionException('You cannot query more than 50000 records.');
        list<QueryResults> result = new list<QueryResults>();
        string query = 'Select '+ queryParams[0].fieldsToQuery + ' FROM ' +  queryParams[0].objectApiName + ' LIMIT ' + queryParams[0].numberOfRecords;
            sObject[] recordList = database.query(query);
            QueryResults qr = new QueryResults();
            qr.records = recordList;
        }catch(Exception e){
            throw e;
        return result;
    /* Input parameters for the Apex action */
    public class QueryParameters{
        @InvocableVariable(label='Api name of the Object' required = true )
        public string objectApiName;
        @InvocableVariable(label='API names of the fields to query(Comma separated)' required = true)
        public string fieldsToQuery;
        @InvocableVariable(label='Number of records to query' required = true)
        public integer numberOfRecords;
    /* Output parameters of the Apex action */
    public class QueryResults{
        @InvocableVariable(label='List of records')
	public sObject[] records;        


And you’re all set to taste that martini! 😀

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

12 thoughts on “Workaround for Query only first N records in Flows – Part 3(Generic solution) #Spring20Delight

  1. Hello! Admin, here. This is really exciting! I’m not understanding how to use it with other criteria. I want the first 500 accounts in North Carolina, for example. Thank you!


  2. Pingback: LIMIT N and WHERE clause together in flows! #Spring20Delight |  forcePanda

  3. Pingback: Update 1000s of Records in Flow with Spring ’20 | Sunshine and Other Unhandled Exceptions

  4. Great approach, and it is working for me in my sandbox. As relative newbie to apex, it would be great to get a sample test class for the QueryNRecords code. Without that, we can’t deploy to production, and these concepts of parameters and results make this a fairly non-standard script against which to create a test class.


    • Hi Keith,
      Since I created this action a while ago, it’s not bulkified. I’d advise to ONLY use this in a screen flow.
      P.S. I can try finding some time to write test for this, no promises though 😅😬


  5. Hey ForcePanda- this is great! Thank you for the awesome content- I’m but an admin so I’m not quite sure how to create a test class. Do you have a test class for this Apex? Much appreciated!


  6. Pingback: Query only first N records in Flows |  forcePanda

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.