1

This question may come off as too vague, and it's very possible it has been answered before, but possibly due to lack of correct terminology, I wasn't able to find anything.

The Situation:

We're building out some automation so that when an Opportunity's StageName is changed to a Closed/Won type, a 3rd party script is being called that should do specific things:

  • Generate promo codes for the purchased products that are used in a separate website (in this case, they are promo codes for certification exams for our products)
  • Email the client the promo codes

To clarify, this isn't an Apex Class or Lightning Component generating these codes as they aren't equipped to create codes that would be valid on our site.

When the specific criteria on the Opportunity is met, we also need to send certain field values over to the script in order for the script to know which promo codes need to be generated (i.e. which certification exams want to be taken).

So SFDC's role would be to house the data on the certifications being purchased and then send that data through an API call to this 3rd party script, and nothing more.

The Question:

My question is this: is it possible to accomplish this through a process builder? I'm expecting the process builder to call some aura enabled Apex method that, in turn, would make the external API call, but I'm not sure if it's possible to do it that way or if it has to be in a trigger?

I apologize if this question comes off as ignorant to how external API calls are supposed to work, but I guess that's why I'm asking this question. :)

If I need to add more information to clarify what I'm trying to do, I can do that! Thanks!

2

You can certainly do this, provided you write an invocable apex method that calls an apex class which implements a Queueable interface and DataBase.AllowCallouts

Here is a simple boiler plate code to get you started

public inherited sharing class ApiCallout {
@InvocableMethod(label='sample api callout' description='example for Process Builder')
public static void invokeAPI(List<Id> objectIds) {
    // Execute Query here
    System.enqueueJob(new RestApiCall(lead));
}
public class RestApiCall implements System.Queueable, Database.AllowsCallouts {
    List<Id> objectIds = new List<Id>() ; 
    public RestApiCall(List<Id> objectIds){
        this.objectIds = objectIds ;
    }
    public void execute(System.QueueableContext ctx) {
        // Query necessary Data
        String body = ''; //populate body
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('<endpoint>'); 
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setMethod('POST');// could be GET/POST
        request.setBody(body);// only needed for POST
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // map to salesforce object
            // update object
        }

    }
   }
}

Please note considerations of Innvocable methods here

  • 1
    I appreciate the explanation. Thank you Mohith! I misspoke when I said aura enabled. I meant invocable so thank you for correcting me on this! I'll take a look into using this! :) – Jacob Barazoto May 7 '20 at 20:06

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.