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.
QueryNRecords.apxc
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;
try{
sObject[] recordList = database.query(query);
//system.debug(recordList);
QueryResults qr = new QueryResults();
qr.records = recordList;
result.add(qr);
}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. 🙂
As always – wonderful article with a useful information!
LikeLike
Very nice! Next step is figuring out how to tackle WHERE clauses!
LikeLike
If you could share your use case, I can try to find a generic solution which can also be used by others.
LikeLike
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!
LikeLike
Hi,
Unfortunately, this one doesn’t support WHERE clause. You will have to create a different Invocable method for that, you can also choose to modify the existing one.
LikeLike
Pingback: LIMIT N and WHERE clause together in flows! #Spring20Delight | forcePanda
Pingback: Update 1000s of Records in Flow with Spring ’20 | Sunshine and Other Unhandled Exceptions
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.
LikeLike
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 😅😬
LikeLike
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!
LikeLike
Hi,
Not for this one unfortunately.
Can you share a little more about your use case?
LikeLike
Pingback: Query only first N records in Flows | forcePanda