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 don’t 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
The first thing you have to do is to create what’s called an Outbound REST Web Service in ServiceNow, which is basically a specification of the call that ServiceNow makes to your application.
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
An outbound REST web service on its own doesn’t do anything; you can invoke it and that’s about it. In order to have it run whenever there’s some new data to push to your application, you’ll need to connect it to 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:
- Select the table the business rule applies to, in this example it’s Incident
- In the When to run tab, check off such that the rule will run on Insert and on Update to the table
- Now check off the Advanced checkbox. This makes the Advanced tab appear.
- The Advanced tab presents you with a script editor showing you a skeleton
executeRulefunction. Recall the outbound REST web service script that you created previously, which you should paste into the body of the function.
Sending the Data
Now the business rule will trigger and run the web service script when an incident is created or updated in ServiceNow. However, it is not actually sending the data yet because we have not set the request body.
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: