0

I am super close to getting this process to work, however, I am a little unclear as to how to deal with the @invocableVariable portion of the process. And just to preface... I am NOT a coder, like at all, so this has been a labor for me.

When I run debug in my flow, it all runs through properly and creates the proper number of records when I change 'LIMIT :NumberOfRecords' to a literal number, like '3'. But I would like to pass a value to the class in an @InvocableVariable titled NumberOfRecords and have it use it in the query.

I have looked at a million examples, but just can't seem to put it to practice.

Any direction would be GREATLY appreciated! Please see my example code below:

public class InvocableAuditDraft {


@InvocableMethod(label='Select Accounts for Audit'
              description='Returns a list of accounts eligible for an Audit.'
              category='Account')    

public static List<List<Account>> getAccountIds(List<ID> ids) {
List<List<Account>> accountList = new List<List<Account>>();
List<Account> accountIds = [SELECT Id FROM Account where Status__c = 'Open' LIMIT 5000];
 
Integer count = accountIds.size();
Integer rand = Math.floor(Math.random() * count).intValue();
List<Account> accounts = [SELECT Id FROM Account WHERE Id in :accountIds LIMIT :NumberOfRecords OFFSET :rand];
accountList.add(accounts);

return accountList;
}

public class FlowInputs {

    @InvocableVariable(required=true)
    public String NumberOfRecords;
}   

}

2
  • I apologize! A little background would have been helpful. I have a flow that will be a button on a Batch Audit screen to allow the user to generate Audit records for Accounts that are eligible for auditing. So, really, the id that is sent to the flow is the Id for the Batch Audit, not an Account. The purpose of the Apex Action is to randomly select accounts for auditing up to a LIMIT of what the user has entered on the Batch Audit screen. The public static List for account ids is not really correct because the ids are for the Batch Audit record.
    – Kris Ryan
    Sep 16, 2022 at 14:01
  • The return of accountList will be a list of the random accounts selected and the flow will use that list to create the draft audit records for each account.
    – Kris Ryan
    Sep 16, 2022 at 14:06

1 Answer 1

1

Your invocable method is defined as:

public static List<List<Account>> getAccountIds(List<ID> ids)

That means that it expects only a list of accountIds as input, one accountId per Flow Interview. (Apex Actions are implicitly bulkified by Flow)

If you want to pass an Id and a LIMIT, you need to change your invocable method signature to:

public static List<List<Account>> getAccountIds(List<FlowInput> inputs)

and change the definition of your FlowInputs class to

public class FlowInput {

  @InvocableVariable(required=true)
  public String NumberOfRecords;

  @InvocableVariable(required=true)
  public Id accountId; 
}

You'll then need to loop through inputs and decide how to process. Each element of input could conceivably have a different row limit so that presents some design challenges as you don't want to do a separate SOQL per input.

It is also not clear to me that you want to pass back a list of accounts per each input accountId but your application requirements are not clear. This also suggests changing the method name to getAccounts

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .