Search code examples
salesforceapexprocessbuilder

I am having a problem with my APEX InvocableMethod returning an error when run through process builder


I have created an invocable method to run through Process builder. Although the class works when triggered with some changes, I get an error when running it through the process builder. From what I have determined, it is happening because the call out isn't run asynchronously.

That being said, I have tried to separate the classes and make one a future call out but I cannot figure out how to pass the three strings I have from the invocable class to my call out class.

Any help would be great!

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) {

        for (DataWrapper dw: passedData) {

        //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());
        }
        }
    }

}


Solution

  • Not sure if you tried Queueable Apex but this is how I do it, give this a shot:

    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());
                    }
                }
            }
        }
    }