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

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.

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

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;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.