Tuesday, November 1, 2016

Debugging an Outbound Message using a public facing Apex REST web service

I needed a way to capture the SOAP Requests that were being sent by an Outbound Message.

One option would be to host something on my local development machine. However, at work this would necessitate getting a network admin to open a port from our public IP address through to my machine.

Another suggestion, by Daniel Hoechst, was to use an existing online service like https://requestbin.com/ to capture the SOAP message.

Instead I thought I'd explore using a public Salesforce Site that exposes a REST web service accepting all POST requests. One advantage of this approach is that the request doesn't need to go off to a third parties server. It's also simple to implement and is mostly just putting a few known parts together.

The first step is to add a public RESTful webservice on the Force.com Site.

The debug log would have been a good option to capture the request, but in Winter '17 a requirement was added to use a cookie to enable the logging - Set a Browser Cookie to Enable Debug Logging for Guest Users. It isn't possible to add a cookie to the outbound message. So, yeah cool. Thanks for that Winter '17 Winking Snowman. (Please vote for the idea: Add IP Whitelisting for Site Guest User Debug Logs)

Instead I've needed to create a basic custom object to store the RestRequest Body.

global with sharing class RESTController {

  global static String inboundSoap() {     
      RestRequest req = RestContext.request;
      DataCapture__c dc = new DataCapture__c();
      dc.Data__c = req.requestBody.toString();
      insert dc;
      return 'Done ' + dc.Id;

Once the Apex class is ready a quick smoke test from POSTMAN to make sure it is working as expected.

Then configure the outbound message to POST to the REST services URL.

Example of a captured outbound message: