Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (13.1k points)
I am trying to create an invocable method for Process builder, but I am getting an error through process builder. Can anyone help me figure this out?

1 Answer

0 votes
by (26.7k points)

You can try it using Queueable Apex, you can take the below example to understand it better.

public class SendText {

    public class DataWrapper {

        @InvocableVariable(label='Correspondence Name' required=true)

        public String CorrespondenceName;

        @InvocableVariable(label='Phone Number' required=true)

        public String PhoneNumber;

        @InvocableVariable(label='Text Message' required=true)

        public String textMessage;

    }

    @InvocableMethod(label='Send Text Message')

    public static void callSendTextMessage (List<DataWrapper> passedData) {

        Id jobId = System.enqueueJob(new TextMessaeQueueable(passedData));

        System.debug('Text messages job Id => ' + jobId);

    }

    private class TextMessaeQueueable implements Queueable, Database.AllowsCallouts {

        private List<DataWrapper> wrappedData;

        public TextMessaeQueueable(List<DataWrapper> passedData) {

            this.wrappedData = passedData

        }

        public void execute(QueueableContext context) {

            for (DataWrapper dw: this.wrappedData) {

                //Basic Info needed to send request

                Http http = new Http();

                HttpRequest request = new HttpRequest();                request.setEndpoint('https://api.podium.com/api/v2/conversations');

                request.setMethod('POST');

                request.setHeader('Content-Type', 'application/json');

                request.setHeader('Accept', 'application/json');

                request.setHeader('Authorization', 'API Key');

                //Create the 4 required fields for Podium

                Map<String, String> message = new Map<String, String>{

                    'customerPhoneNumber' => dw.PhoneNumber,

                    'message' => dw.textMessage,

                    'locationId' => '49257',

                    'customerName' => dw.CorrespondenceName

                };

                String messageJson = JSON.serialize(message);

                System.debug(messageJson);

                request.setBody(messageJson);

                HttpResponse response = http.send(request);

                // Parse the JSON response

                if (response.getStatusCode() != 201) {

                    System.debug('The status code returned was not expected: ' +

                        response.getStatusCode() + ' ' + response.getStatus());

                } else {

                    System.debug(response.getBody());

                }

            }

        }

    }

}

I hope this will work.

Want to become a Salesforce expert? join salesforce developer certification now!!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...