1

Can anyone help in providing an example of an APEX class which can make a HTTP JSON POST request to an external webservice using a plugin for SF visual workflow?

Example I tried following here

Similar to the example above, what I'm trying to do is create a flow which will take X number of variables from a flow, turn them into JSON and POST it to a 3rd party web service such as Zapier.

UPDATE: I was able to get the below code to send a POST request to an endpoint like Zapier. It's pretty messy (I'm not an APEX dev), so would love for someone to offer any improvements for others.

global class PostPlugin implements Process.Plugin { 

        // The main method to be implemented. The Flow calls this at runtime.
        global Process.PluginResult invoke(Process.PluginRequest request) { 
                // Get the variables from the flow
                String var1 = (String) request.inputParameters.get('var1');
                String var2 = (String) request.inputParameters.get('var2');
                String Var3 = (String) request.inputParameters.get('var3');
                String Var4 = (String) request.inputParameters.get('var4');
                String PostURL = (String) request.inputParameters.get('PostURL');

                // Serialize variables to JSON
                Map<String, String> body = new Map<String, String>();
                body.put('Variable 1', var1);
                body.put('Variable 2', var2);
                body.put('Variable 3', var3);
                body.put('Variable 4', var4);
                String sbody = JSON.serialize(body);

                // Instantiate a new http object
                 Http h = new Http();

                // Instantiate a new HTTP POST request
                 HttpRequest req = new HttpRequest();
                 req.setEndpoint(PostURL);
                 req.setMethod('POST');
                 req.setHeader('Content-Type', 'application/json;charset=UTF-8');
                 req.setBody(sbody);


                // Send the request, and return a response
                HttpResponse res = h.send(req);

                // return to Flow
                Map<String,Object> result = new Map<String,Object>();
                return new Process.PluginResult(result);

                }

            global Process.PluginDescribeResult describe() { 
                Process.PluginDescribeResult result = new Process.PluginDescribeResult(); 
                result.Name = 'postappplugin';
                result.Tag = 'postapp';
                result.inputParameters = new 
                   List<Process.PluginDescribeResult.InputParameter>{ 
                       new Process.PluginDescribeResult.InputParameter('var1', Process.PluginDescribeResult.ParameterType.STRING, true) ,
                       new Process.PluginDescribeResult.InputParameter('var2', Process.PluginDescribeResult.ParameterType.STRING, true) ,
                       new Process.PluginDescribeResult.InputParameter('var3', Process.PluginDescribeResult.ParameterType.STRING, true) ,
                       new Process.PluginDescribeResult.InputParameter('var4', Process.PluginDescribeResult.ParameterType.STRING, true) ,
                       new Process.PluginDescribeResult.InputParameter('PostURL', Process.PluginDescribeResult.ParameterType.STRING, true) 
                    };
                 return result; 
            } 

        }
  • Maybe you can try, provide minimal code samples and explain to us what you dont understand, or where you are stuck? – glls May 22 '17 at 19:30
  • I'm honestly stuck on the whole thing - Zero background in apex, so just trying to mesh something together which can post some variables to an endpoint in a flow. Other than that one blog post i'm struggling to find the info or decent examples to work off. – Moo Master May 22 '17 at 19:39
  • Ping @MohithShrivastava, the author of the linked post. – Daniel Ballinger May 22 '17 at 20:14
0

one improvement i see is to switch to invocable method, and here is why:

We recommend using the @InvocableMethod annotation instead of the Process.Plugin interface. The interface doesn’t support Blob, Collection, sObject, and Time data types, and it doesn’t support bulk operations. Once you implement the interface on a class, the class can be referenced only from flows. The annotation supports all data types and bulk operations. Once you implement the annotation on a class, the class can be referenced from flows, processes, and the Custom Invocable Actions REST API endpoint.

Link to guide:

Implementing the Process.Plugin Interface

InvocableMethod Annotation

and here is small example of invocable method:

public with sharing class FlowDataInvocable {
    @InvocableMethod(label='' description='')
    public static List<String> searchCampaigns(List<FlowData> inputDataList) {
        // collect params
        Map<String, String> body = new Map<String, String>();
        String reqEndpoint;
        for (FlowData inputData : inputDataList) {
            // get the data, assumption we will have only one elememt in inputDataList
            body.put('Variable 1', inputData.var1);
            body.put('Variable 2', inputData.var2);
            body.put('Variable 3', inputData.var3);
            body.put('Variable 4', inputData.var4);
            reqEndpoint = inputData.reqEndpoint;
        }

        String sbody = JSON.serialize(body);
        // Instantiate a new http object
        Http h = new Http();
        // Instantiate a new HTTP POST request
        HttpRequest req = new HttpRequest();
        req.setEndpoint(reqEndpoint);
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json;charset=UTF-8');
        req.setBody(sbody);
        // in case we need to pull back response from 3rd party to flow
        List<String> response = new List<String>();
        try {
            // Send the request, and return a response
            HttpResponse res = h.send(req);
            response.add(res.getBody());
        }
        catch (Exception e) {
            response.add('error');
        }
        return response;
    }

    public class FlowData {
        @InvocableVariable(label = '' required = true)
        public String var1;
        @InvocableVariable(label = '' required = true)
        public String var2;
        @InvocableVariable(label = '' required = true)
        public String var3;
        @InvocableVariable(label = '' required = true)
        public String var4;
        @InvocableVariable(label = '' required = true)
        public String reqEndpoint;
    }
}

UPD: here is the link for how to test callouts Testing HTTP Callouts

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.