Outbound REST interface in ServiceNow


A very common case in any enterprise development is to bind one piece of software to another. Whether it is an SAP, which needs to post tickets to Jira, or Dynamics, willing to publish data in SharePoint – it is quite often a case, when we want to use web services. This article will be describing a simple case of outbound web service development in ServiceNow.

Suppose, each time user creates an Incident, we want our ServiceNow to call external system through REST Service. Say, we pass an Incident ID and Description as parameters.

First we would want to create a message template under “System Web Services” -> “REST Message” -> “New”. Give it an appropriate name and enter the external Webservice URL as an Endpoint

 

You would want to configure an authentication for production use, but I will call an anonymous Echo service for simplicity. After inserting REST Message, go to HTTP Methods Section and create a new POST method:

At this point you can configure the format of outgoing message and its variables. We use JSON, therefore create an HTTP Header “Content-Type” with value “application/json”.

Then we specify our JSON message body, a simple one, containing only two variables

{
“id”: “${id}”,
“description”: “${description}”
}

After saving the message, you can click “Auto-generate variables” right above the “Variable Substitutions” section and ServiceNow will automatically recognize our tokens in curly braces and populate substitution list with them.

The REST configuration is over, now we go to Incidents List, open any incident, and choose to configure Business Rules in top left menu:

Click “New” Button to create a new business rule, give it appropriate name and select you want this rule to be ran on Insert:

After the rule is created, check the “Advanced” box, and go to “Advanced” settings tab. Now it is time for some scripting (just a little bit, I promise). Copy and paste the following snippet to “Script” field.

 

(function executeRule(current, previous /*null when async*/) {
// load a pre-defined message template
var message = new sn_ws.RESTMessageV2(‘MySimpleRest’, ‘MyPost’);
// set previously defined variables
message.setStringParameter(“id”, current.number);
message.setStringParameter(“description”, current.short_description);
// call webservice and write results to log
var response = message.execute();
gs.log(‘MyNotification:\n ‘ + message.getRequestBody() +
‘\n\nRest status:\n’ + response.getStatusCode() +
‘\n\nResponse:\n’ + response.getBody());
})(current, previous);

 

The functionality is pretty self-explanatory, we load the previously configured template, fill it with current incident properties and send the Webservice request right away. The correctness and return status of the request can be controlled in System Log.

Now create a new incident and check out the recent logs. It will contain an entry, looking like this:

That indicates, the goal is achieved, Webservice triggers upon Incident creation and pushes data to the external REST interface. The above sample is good for any public Webservices, “visible“ in Internet. If you intend to call internal Services behind the corporate firewall, you need to have a MID Server and configure our REST message to use this server. Just as easy as that!

 
Bild von Uladzimir Astapchyk
Uladzimir Astapchyk Uladzimir Astapchyk ist seit 2016 bei HanseVision und seit 2000 als Softwareentwickler tätig und dabei zuständig für die Entwicklung von individuellen Lösungen und Integrationsprojekte in Microsoft-Umfeld. In diesem Bereich analysiert er Anforderungen, implementiert Komponenten zusammen mit seinen Kolleg:innen und den Kund:innen. Alle Artikel des Autors

Ähnliche Blog-Artikel

Mit unserem HanseVision Update sind Sie immer gut informiert über alle Themen rund um moderne Zusammenarbeit, kluge Köpfe, Lösungen und Tools, Referenzen und Aktionen.

Jetzt zum Newsletter anmelden
Updates & Aktionen
Versand alle 4-6 Wochen
Trends & aktuelle Entwicklungen