documentation logo
All you’ll ever need to know about Declarative Webhooks and how to get started.

Pages

Implement a Callout Using Apex

Make a Declarative Webhooks call with apex

You can use Apex to make a callout using a callout template. Below are some methods you can use to make callouts. All these methods use the callout template developer name to identify a template, which is a field automatically generated when you create or edit a callout template. You can find it either in the callout template page layout, or in the Declarative Webhooks app -> Administration tab -> Callout Templates subtab.

  • d_wh.CalloutsManager.CalloutResponse res = d_wh.CalloutsManager.MakeCalloutDN(templateDevName);
    • Use this method to make a callout using a callout template with no main object selected.
    • templateDevName is the callout template developer name.
    • Returns a callout response. See details about CalloutResponse below.
  • d_wh.CalloutsManager.CalloutResponse res = d_wh.CalloutsManager.MakeCalloutDN(templateDevName, recordId);
    • Use this method to make a callout using a callout template with a single record.
    • templateDevName is the callout template developer name.
    • recordId is the Salesforce Id of the main record to use in this callout.
    • Returns a callout response. See details about CalloutResponse below.
  • d_wh.CalloutsManager.CalloutResponse res = d_wh.CalloutsManager.MakeCalloutDN(templateDevName, recordIds);
    • Use this method to make a callout using a callout template with multiple records (batch).
    • templateDevName is the callout template developer name.
    • recordIds is a List with Salesforce Ids of the records to use in this callout.
    • Returns a callout response. See details about CalloutResponse below.
  • d_wh.CalloutsManager.MakeCalloutAsyncDN(templateDevName);
    • Similar to the MakeCalloutDN(templateDevName), except it’s a @future method and starts asynchronously. For that reason, it does not return a response.
  • d_wh.CalloutsManager.MakeCalloutAsyncDN(templateDevName, recordId);
    • Similar to the MakeCalloutDN(templateDevName, recordId), except it’s a @future method and starts asynchronously.  For that reason, it does not return a response.
  • d_wh.CalloutsManager.MakeCalloutAsyncDN(templateDevName, recordIds);
    • Similar to the MakeCalloutDN(templateDevName, recordIds), except it’s a @future method and starts asynchronously.  For that reason, it does not return a response.

 

The following methods are using the callout template Salesforce Id to identify the callout template to use. To avoid hardcoding record ids in your code, we recommend using the methods above, which use the callout template developer name to identify the callout template.

  • d_wh.CalloutsManager.CalloutResponse res = d_wh.CalloutsManager.MakeCallout(templateId);
    • Use this method to make a callout using a callout template with no main object selected.
    • templateId is the Salesforce Id of the callout template to use.
    • Returns a callout response. See details about CalloutResponse below.
  • d_wh.CalloutsManager.CalloutResponse res = d_wh.CalloutsManager.MakeCallout(templateId, recordId);
    • Use this method to make a callout using a callout template with a single record.
    • templateId is the Salesforce Id of the callout template to use.
    • recordId is the Salesforce Id of the main record to use in this callout.
    • Returns a callout response. See details about CalloutResponse below.
  • d_wh.CalloutsManager.CalloutResponse res = d_wh.CalloutsManager.MakeCallout(templateId, recordIds);
    • Use this method to make a callout using a callout template with multiple records (batch).
    • templateId is the Salesforce Id of the callout template to use.
    • recordIds is a List with Salesforce Ids of the records to use in this callout.
    • Returns a callout response. See details about CalloutResponse below.
  • d_wh.CalloutsManager.MakeCalloutAsync(templateId);
    • Similar to the MakeCallout(templateId), except it’s a @future method and starts asynchronously. For that reason, it does not return a response.
  • d_wh.CalloutsManager.MakeCalloutAsync(templateId, recordId);
    • Similar to the MakeCallout(templateId, recordId), except it’s a @future method and starts asynchronously.  For that reason, it does not return a response.
  • d_wh.CalloutsManager.MakeCalloutAsync(templateId, recordIds);
    • Similar to the MakeCallout(templateId, recordIds), except it’s a @future method and starts asynchronously.  For that reason, it does not return a response.

 

The response from the MakeCallout and MakeCalloutDN methods is a d_wh.CalloutsManager.CalloutResponse class. It contains various information about the callout that was made:

  • string preCalloutURL – if the callout has a pre-callout, this is populated with the pre-callout URL
  • string preCalloutMethod – if the callout has a pre-callout, this is populated with the pre-callout method
  • string preCalloutBody – if the callout has a pre-callout, this is populated with the pre-callout request body
  • List<d_wh.CalloutsManager.RequestHeader> preCalloutHeaders – if the callout has a pre-callout, this is populated with the pre-callout list of headers
  • integer preCalloutResponseCode – if the callout has a pre-callout, this is populated with the pre-callout response code
  • string preCalloutResponseBody – if the callout has a pre-callout, this is populated with the pre-callout response body
  • Id preCalloutLogId – if the callout has a pre-callout, this is populated with the Salesforce Id of the log record
  • string calloutURL – this is populated with the callout URL
  • string calloutMethod – this is populated with the callout method
  • string calloutBody – this is populated with the callout generated request body
  • List<d_wh.CalloutsManager.RequestHeader> calloutHeaders – this is populated with the callout list of sent headers
  • integer calloutResponseCode – this is populated with the callout received response code
  • string calloutResponseBody – this is populated with the callout received response body
  • Id calloutLogId – this is populated with the Salesforce Id of the generated log record
  • List<sObject> updatedRecs – this is populated with the list of Salesforce records that were created / updated as a result of the callout, based on response actions defined for the used callout template (except for Apex response actions)

 

Test class support

If you are making a callout using the Apex methods above, you will need to write a test class to cover the code. In which case, you need to set a HttpCalloutMock class for the callout. If the code that performs the callout is in a managed package, the Test.setMock call needs to be done from a test method in the same package with the same namespace. So instead of the usual:

Test.setMock(HttpCalloutMock.class, mock);

you can do it like this:

d_wh.CalloutsManager.setMock(mock);

where mock is an instance of a class that implements HttpCalloutMock, as described in the Salesforce documentation ( https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm )