1

I have a flow from which I want to call an apexclass and retrieve a list of picklistvalues so a user can select from these in a flowscreen. How do I achieve this. I tried the code below but when I use the apex action type in the flow I do not get the variable "lstCancelOptions"

Apex

public class getCancelOptionsCurrentUser {
    @InvocableMethod(label = 'Get cancel options current user' description = 'Get amendment Cancel reasons')
    public static List<String> getCancelOptionsMsPicklist() {
    String CancelOptions;
    List<String> lstCancelOptions = new List<String>();
        Id CurrUser = UserInfo.getUserId();
        try {
            CancelOptions = [SELECT Id, Reasons_Cancel__c from User where Id = :UserInfo.getUserId()].get(0).Reasons_Cancel__c;
        } Catch(Exception e) {
            System.debug('Error Canceloptions current user: ' + e.getMessage());
        }
        if (String.isNotBlank(CancelOptions)) lstCancelOptions = CancelOptions.split(';');
        return lstCancelOptions;
    }
}

3 Answers 3

3

The Flow mechanism is bulkified. This means if you return a List<String>, it will map to a single variable, while List<List<String>> will map to a collection variable. You'll need to adjust your code to use List<List<String>> in order for this to work.

3
  • so do I do: public static List<List<String>> getCancelOptionsMsPicklist() { and return List<new List<String>{lstCancelOptions}>; ???
    – Thomas
    Commented Apr 6, 2020 at 12:13
  • @Thomas new List<List<String>>{lstCancelOptions};, but yes, that's basically what you need to do.
    – sfdcfox
    Commented Apr 6, 2020 at 12:14
  • Thanks that was the answer.
    – Thomas
    Commented Apr 6, 2020 at 13:21
1

Alternatively, a wrapper class can be maintained for flow variables. The variables can be marked with the annotation @InvocableVariable. Multiple variables can be returned from the method and also passed as params to the method. All invocable variables are available for selection in the flow.

@InvocableMethod(label = 'Get cancel options current user' description = 'Get amendment Cancel reasons')
public static List<FlowOutput> getCancelOptionsMsPicklist(){
    List<FlowOutput> flowOutput = new List<FlowOutput>();
    FlowOutput output = new FlowOutput();
    String cancelOptions;
    List<String> lstCancelOptions = new List<String>();
    Id CurrUser = UserInfo.getUserId();
    try{
        cancelOptions = [SELECT Id, Reasons_Cancel__c from User where Id = :UserInfo.getUserId()].get(0).Reasons_Cancel__c;
    } catch(Exception e) {
        System.debug('Error Canceloptions current user: ' + e.getMessage());
    }
  if (String.isNotBlank(cancelOptions)) lstCancelOptions = cancelOptions.split(';');
    output.listOfCancelOptions = lstCancelOptions;
    flowOutput.add(output);
    return flowOutput;
}
public class FlowOutput{
    @InvocableVariable()
    public List<String> listOfCancelOptions;
}
0

One key point is that when you added the Apex to the flow it will create a variable in the flow for the return. if the name on the label is lstCancelOptions there will be a variable named lstCancelOptions.

You must log in to answer this question.

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