How to catch Salesforce Outbound Messages with NodeJS?

Catching salesforce outbound messages with NodeJS
Table of Contents

What is Outbound Messaging?

  • Outbound messaging allows you to specify that changes to fields within Salesforce can cause messages with field values to be sent to designated external servers.

blog img

  • Outbound messaging is part of the workflow rule functionality in Salesforce. Workflow rules watch for specific kinds of field changes and trigger automatic Salesforce actions, such as sending email alerts, creating task records, or sending an outbound message.

When building applications that are integrated with Salesforce (Salesforce Integration), one of the choices you have to make is how you get data out of Salesforce and into your app. You can use one of the many ETL tools on the market, you can poll for changed records, use the Force.com Streaming API, use Apex HTTP Callouts from Salesforce or Outbound Messages.

However, with that said, Outbound Messages are quite magical. You hook them up as an action to your Workflow Rule so that whenever a record is, for example, created or updated in some manner, the platform will fire off some record data to the endpoint specified in the Outbound Message.

So here’s a simple NodeJS app that will receive the XML from your Outbound Message and convert it into a JavaScript object that you can then use to do all sorts of awesome stuff!

var express = require('express');
var _ = require("lodash");
var router = express.Router();
router.post('/', function(req, res) {

// get the obm as an object
var message = unwrapMessage(req.body);
if (!_.isEmpty(message)) {

// some something #awesome with message
console.log(message);

// return a ‘true’ Ack to Salesforce
res.send(
'soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:out="http://soap.sforce.com/2005/09/outbound">true</soapenv:Envelope'
);
} else {
// return a ‘false’ Ack to Salesforce
res.send(
'soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:out="http://soap.sforce.com/2005/09/outbound">false</soapenv:Envelope'
);
}
});

// unwrap the xml and return object
unwrapMessage = function(obj) {
try {
var orgId = obj['soapenv:envelope']['soapenv:body'][0].notifications[0].organizationid[0];
var contactId = obj['soapenv:envelope']['soapenv:body'][0].notifications[0].notification[0].sobject[0]['sf:id'][0];
var mobilePhone = obj['soapenv:envelope']['soapenv:body'][0].notifications[0].notification[0].sobject[0]['sf:mobilephone'][0];
return {
orgId: orgId,
contactId: contactId,
mobilePhone: mobilePhone
};
} catch (e) {
console.log('Could not parse OBM XML', e);
return {};
}
};
module.exports = router;

Now we need to set up the Workflow and Outbound Message that will send the data to your NodeJS application. You’ll want to change the Workflow to meet your criteria as this one fires every time a Contact record is created or updated and the mobile phone is not blank.

blog img

The actual Outbound Message below defines the endpoint (you’ll notice I’m running it locally using ngrok), the fields from the record to include in the message (the record Id and MobilePhone) and finally “Send Session ID” is checked. You’ll probably want to include the Session ID so that you can grab it from the message and use it to quickly make calls back into Salesforce using something like nforce.

blog img

If you want to test the application locally you can use something like ngrok to securely expose your local web server. Salesforce (thankfully) requires that all endpoints use HTTPS so ngrok comes in handy.

Now if you modify a Contact record in Salesforce, you should see the following in your terminal if everything is configured correctly:

blog img

There’s also a mocha test you can run to ensure that the XML is being parsed correctly.

There you have it… a simple NodeJS app to get you started using Outbound Messages from Salesforce.

Get thoughtful updates on what’s new in technology and innovation

    Want to build CRM Solutions with Salesforce?

    Share it:
    As the Head of Technology at GetOnCRM, I thrive on transforming complex technical landscapes into scalable, business-first solutions. With years of experience leading innovative projects, I’m passionate about leveraging Salesforce and emerging technologies to help organizations achieve seamless digital transformation. My focus is always on creating a synergy between technology and business goals.