[SalesForce] Using Invocable Apex in Flow Builder, can’t save result

I have a flow that will be used to convert a list view of selected records of unknown type to custom object records. When the flow launches from a list view page, there will be a screen element where the user can select Field Labels from whatever object it is, to be mapped to FirstName, LastName, and MobilePhone fields.

I'm using invocable apex, called from the flow, to return a list of field label names (of type TEXT or PHONE) that will populate a pulldown menu so the user can select the right field to be mapped.

Here's one of my invocable apex classes:

global with sharing class SchemaFirstName {
@InvocableMethod(label='Get First Name options for this recordId')
global static List<String> getFirstNameFieldLabels(List<String> recordIds){
    Id recordId = recordIds[0];
    List<String> firstNameLabels = new List<String>();
    Boolean needFirstList = true;
    DescribeSObjectResult describeResult = recordId.getSObjectType().getDescribe();
    Map<String, Schema.SObjectField> fieldMap = describeResult.fields.getMap();
    Set<String> fieldNames = fieldMap.keySet();
    if(fieldNames.contains('firstname')){
        firstNameLabels.add(fieldMap.get('firstname').getDescribe().getLabel());
        needFirstList = false;
    }else if(fieldNames.contains('firstname__c')){
        firstNameLabels.add(fieldMap.get('firstname__c').getDescribe().getLabel());
        needFirstList = false;
    }

    Schema.DisplayType stringType = Schema.SObjectType.Contact.fields.getMap().get('firstname').getDescribe().getType();

    if(needFirstList){
        for(String fieldName : fieldNames){
            if(fieldMap.get(fieldName).getDescribe().getType() == stringType){
                if(needFirstList) firstNameLabels.add(fieldMap.get(fieldName).getDescribe().getLabel());
            }
        }
    }
    return firstNameLabels;
}

}

The invocable apex shows up in the Flow Builder, and I can specify the input list of Ids. I have created a collection variable in the Flow, firstNameOptions, to accept the list that is returned.
enter image description here

However, when I try to create the Apex module in the flow, it won't recognize the firstNameOptions variable I created.

enter image description here

I've searched the docs and they don't seem to get into detail about using invocable apex and handling results.

Best Answer

I have copied and pasted your apex code in my org. Created firstNameOptions text variable in flow and it shows up under Apex Action.Please see below screenshot.

Deselect Allow Multiple values(Collection)

enter image description here enter image description here

Related Topic