michaelwS
michaelwS

Reputation: 15

How to pass Salesforce Flow variables into Apex Class with correct scope?

I'm trying to use a use a Flow in Salesforce to pass variables into an Apex Class that performs an HTTP Post to an external API. The Flow grabs an Opportunity, parses some of its fields, and inputs them into the Apex class. From there, the Apex class calls an InvocableMethod to create a custom object to store these inputs as a list of strings and then pass them to a 'webservice' class that creates a JSON object of the data and POST it to the API.

I'm able to get the POST to work properly with hard-coded strings but when I try to use the input variables the Flow errors out saying that The input parameter "xxxxxx" isn't available in the referenced action. Remove it from the "Apex-Class-xxxx" Action element.

In the debug screen of the Flow I can also see that these inputs are grabbing the correct values from Salesforce but it errors out saying: An invalid input parameter was specified for Apex action

How do I get these inputs to properly go into the Apex class?

global class MyApexClass
{
    //Stores output from 'sendToWebService' to be converted to JSON
    public class Body {
      public String address;
      public String companywebsite;
      public String companyname;
      public string opportunityid;

    }
    //Stores inputs from Flow 
    public class FlowInputs {
        @InvocableVariable public String Address;
        @InvocableVariable public String CompanyWebsite;
        @InvocableVariable public String CompanyName;
        @InvocableVariable public String OppId;
    }
    //Method to call webservice method since InvocableVariables can't be passed to it directly
    @InvocableMethod(label='xxxx' description='xxxxx')
    public static List<String> sendToWebService(List<String> flowData){
        List<String> outList;

        for (String input : flowData){
            outList.add(input);
        }
        
        SendToAPI(outList);
    
        return outList;
    }

    //creates an object to store inputs, converts to JSON, and sends it to the API via HTTP POST
    webservice static void SendToAPI(List<String> accountData){
        
        Http m_http = new Http();
        HttpRequest req = new HttpRequest();
        .........

Upvotes: 1

Views: 4581

Answers (1)

Jay Raleigh
Jay Raleigh

Reputation: 26

You're basically there -

On the invocable method, you need to declare a list/array of the apex object you made to store the inputs, not a list of strings:

@InvocableMethod(label='xxxx' description='xxxxx')
public static List<String> sendToWebService(List<FlowInputs> request)

Then, you should be able to do the formatting of the json within the request header/body method itself

Upvotes: 1

Related Questions