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. 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. 🙂

.png)




Comments