2

I'm using an apex action to obtain, from a recordId (which is the input variable that is sent from the flow into apex), the name of the object to which it belongs (objectname). I want to send this variable back into the flow as an input variable, but I'm not sure how to do it.

Here's my code at the moment - after the method which obtains the name of the object, I have created another class called "FlowOutputs" that includes an @InvocableVariable objectname. However, I suspect that the variable is not being sent from the method to the "FlowOutputs" class.

Here is my code:

public class GetObjectNameFromRecordId {
    public class FlowInputs{
        @InvocableVariable public String recordId;
    }
    @InvocableMethod public static void GetObjectNameFromRecordId(List<FlowInputs> ParameterList){ 
        Id recordId2getobject = ParameterList[0].recordId;
        String objectname = recordId2getobject.getSObjectType().getDescribe().getName();
    }
    public class FlowOutputs{
        @InvocableVariable public String objectname;
    }
}

I want to be able to see this option in the Action, in order to store the output value (in my case the object name):

enter image description here

Thanks very much!

2 Answers 2

4

Your invocable method should be bulkified and return, for each input in the collection, an output in a collection.

public class ObjectNameFromIdInvocable {
  public class FlowInput{
    @InvocableVariable public Id recordId;
  }

  @InvocableMethod public static FlowOutput[] getObjectNameFromRecordId(List<FlowInput> params){ 
    FlowOutput[] results = new List<FlowOutput>();
    for (FlowInput param: params) {
       results.add(new FlowOutput(
           param.recordId.getSObjectType().getDescribe().getName()));
    }
    return results;
  }

  public class FlowOutput{
    @InvocableVariable public String objectname;
    public FlowOutput(String objName) {this.objectName = objName;}
  }
}

Note that I renamed your inner classes as they represent a single input/output from a single Flow Interview - but the invocable method is called with all Flow Interviews in one bulk operation (hence why it accepts a list of inputs and returns a list of outputs.

I also renamed the class to include Invocable in the name which makes them easier to find later.

2
  • Thanks, this worked great! I did two small changes because Salesforce was giving me errors with the code: Change1: Row 3 - changed "String recordId" for "Id recordId" - recordId has to be an Id type variable for "getSObjectType()" to work later on. || Change2: Row 6 - changed "List<FlowInputs>" to "List<FlowInput>", without the s, as the name of the list had been changed before. Thanks again :)
    – bonzi
    Commented Jun 2, 2021 at 10:08
  • Yep. I was a bit sloppy
    – cropredy
    Commented Jun 2, 2021 at 15:11
2

Your @InvocableMethod needs a return type other than void. Whatever you return will be your Apex output that will be accessible to the Flow Action.


EDIT:

Invocable methods are a beast to work with (hence me having to redo my entire answer). According to the documentation, inputs and output for invocable methods must be a List. You have the inputs taken care of, you need to output a List.

I haven't tested this, but I did not get barked at because of this formatting:

@InvocableMethod public static List<FlowOutputs> GetObjectNameFromRecordId(List<FlowInputs> ParameterList){
   List<FlowOutputs> result = new List<FlowOutputs>();
   FlowOutputs output = new FlowOutputs();
   Id recordId2getobject = ParameterList[0].recordId;
   String name = recordId2getobject.getSObjectType().getDescribe().getName();
   output.objectname = name;
   result.add(output);
   return result;
}

Notice I changed the method return type from void to List<FlowOutputs>, then instantiated a list of FlowOutputs and a specific instance of FlowOutputs within the method.

3
  • Thanks @Moonpie, but I'm getting the following message: "InvocableMethod methods do not support return type of String". What do you mean with "whateverYouWantToNameYourReturnVariable"? Shouldn't the name of the Method be included just after the data return type?
    – bonzi
    Commented Jun 1, 2021 at 14:56
  • Yeah, I just realized I screwed up and answered too quicky. Sorry. Working on updating my answer.
    – Moonpie
    Commented Jun 1, 2021 at 14:58
  • I just slowed down and reworked. Hopefully that does it.
    – Moonpie
    Commented Jun 1, 2021 at 15:34

You must log in to answer this question.

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