ServiceNow Webhooks

Recently I found myself wanting to get data out of ServiceNow. Typically, there are 2 ways to get data out of a platform: you either poll the data, or the data is pushed to you. In the latter case platforms provide webhooks that you’d configure to point to your application that’s ready to receive the data.

ServiceNow can push data to you but they dont call it webhooks. You actually have to stitch together a couple of ServiceNow features and concepts in order to achieve what typical webhooks do. On one hand, it gives the developer great flexibility to customize what data they can get out of the platform, but on the other hand it’s not quite straightforward to setup and took a bit of googling to figure out. I didn’t find a single place where all the steps were laid out plainly, which is why I decided to write this post about it.

Outbound REST Web Service

This doc page outlines the steps you’d take to tell ServiceNow the URL your application is listening on, the HTTP method to use, the headers and query parameters to include in the request, and so on. The last step described is to generate a preview of the script that makes the call — this is important, you’ll need to copy the script to use later.

Creating a Business Rule

The instructions for creating a business rule is quite lengthy and there are many ways you can configure the rule. But let’s say you just want to be notified whenever an incident is opened or updated in ServiceNow, here’s what you do:

  1. Select the table the business rule applies to, in this example it’s Incident
  2. In the When to run tab, check off such that the rule will run on Insert and on Update to the table
  3. Now check off the Advanced checkbox. This makes the Advanced tab appear.
  4. The Advanced tab presents you with a script editor showing you a skeleton executeRule function. Recall the outbound REST web service script that you created previously, which you should paste into the body of the function.

Sending the Data

In the executeRule function, the current variable holds the object that was created or updated. To get a JSON representation of that object to send to your application, you might be tempted to use JSON.stringify on it but it’s not going to work. As it turns out, the object is a GlideRecord and you’ll need to iterate through its fields to build the JSON yourself. Therefore, the last step to make everything work is to modify your business rule script to build the JSON and set it into the request:

Links