Process Builder allows you to easily automate business processes without any code, using a graphical interface to build processes. However, it is limited to actions within Salesforce. To communicate with external web services, you will have to use more advanced features like an Apex code. This document shows you how to create an Apex class to communicate with a Workato Callable Recipe.
We will not go into too much details about Apex. However, here are a few things you need to be aware of.
- Uses HttpRequest class to communicate with Workato Callable recipes exposed as REST endpoints.
- To expose an Apex class method to Process Builder, you must annotate each one with
@InvocableMethod
. - Web Service callouts are not allowed to occur after a DML statement within the same transaction/thread. These callouts must be executed asynchronously.
- To do that, HTTP Requests must be annotated with
@future
. The method now executes when Salesforce has available resources.
public class HTTPRequest {
@InvocableMethod(label='sendHTTPRequest' description='Trigger Workato callable recipe.')
public static void sendRequest(List<Id> ids) {
System.Debug('triggered');
for (Id id : ids) {
callRecipe(id);
}
}
@future(callout=true)
private static void callRecipe(Id id) {
System.HttpRequest req = new System.HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://www.workato.com/service/eeshansim/lead/new');
req.setHeader('API-TOKEN', 'YOUR_API_TOKEN');
req.setHeader('Content-Type', 'application/json');
req.setBody('{\"id\":\"' + id + '\"}');
Http myHttp = new Http();
HttpResponse res = myHttp.send(req);
}
}
This Apex performs a HTTP POST
request to a callable recipe which is exposed as a REST endpoint. This request carries the record ID
in it's payload to indicate the record of interest to the recipe. This ID
value can then be used by the recipe to perform a variety of tasks.
Note: Workato REST endpoints use token authentication in Headers
Add a new Apex action in your Process Builder and select this new Apex class method you created.
The callable recipe must be enabled as a REST endpoint. For this example, the Recipe only expects a single field id
in the JSON payload. This refers to the ID
of the Lead record being proccessed in the Process Builder. Make sure the Callable Recipe is started to activate the endpoint. More information about Callable Recipes here.
Navigate to API Gateway in Workato to view all Callable recipes exposed as API endpoints.
Remember to add the callable recipe endpoint as a remote endpoint. Without it, you will recipe this error message:
EXCEPTION_THROWN [19]|System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://www.workato.com/service/eeshansim/lead/new
To do this, navigate to Setup
> Security
> Remote site settings
and add a New Remote Site
. You can find the callable recipe endpoint under API Gateway
in your Workato account.