Salesforce – Webhook Setup

Add Webhook class to Salesforce account

  1. Log in into your Salesforce Account, navigate to the top right corner, select the Cog icon, and select Setup.
  2. Navigate to the left panel, scroll down and select Custom Code, and then Apex Classes
  3. Select New to create a new Apex Class.
  4. Name the class Webhook and paste the following code snippet:

  public class Webhook implements HttpCalloutMock {

    public static HttpRequest request;
    public static HttpResponse response;

    public HTTPResponse respond(HTTPRequest req) {
        request = req;
        response = new HttpResponse();
        response.setStatusCode(200);
        return response;
    }

    public static String jsonContent(List<Object> triggerNew, List<Object> triggerOld) {
        String newObjects = '[]';
        if (triggerNew != null) {
            newObjects = JSON.serialize(triggerNew);
        }

        String oldObjects = '[]';
        if (triggerOld != null) {
            oldObjects = JSON.serialize(triggerOld);
        }

        String userId = JSON.serialize(UserInfo.getUserId());

        String content = '{"new": ' + newObjects + ', "old": ' + oldObjects + ', "userId": ' + userId + '}';
        return content;
    }

    @future(callout=true)
    public static void callout(String url, String content) {

        if (Test.isRunningTest()) {
            Test.setMock(HttpCalloutMock.class, new Webhook());
        }

        Http h = new Http();

        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(content);

        h.send(req);
    }
}

Add service domain

To add a service domain, go to your Cyclr console:

  1. Go to Settings > General Settings
  2. Copy your Service Domain.
  3. Login into your Salesforce Account, navigate to the top right corner and click the Clog icon, then click Setup.
  4. Navigate to the left panel, scroll down and click Security, then click Remote Site Settings
  5. Select New Remote Site, enter a name and add your Service Domain from step 2.