ServiceNow has always been great to make traditionally difficult integration interfaces easy to setup and execute. This typically translates into a lot of point and click activities.

For the die-hard coders, like me, sometimes we want a little less point and clickedness to get our job done.

For example, if you want to consume a REST based API that has many different API calls, you would traditionally have to create several REST Message records within the ServiceNow platform. Each record would have its own function headers, endpoint, request parameters, variables, etc. While it makes it easy to understand, it can introduce a lot of steps.

In order to bypass the UI and go straight into REST-based API consumption, I have created a Script Include that extends the current “RESTMessage” library and adds 100% scripting capabilities. This library is called “RESTMessageScripted”.

Documentation

RESTMessageScripted( httpFunctionName, endpointURL )
Cosntructor
httpFunctionName: “get”, “put”, “post”, “delete”
endpointURL: the full URL string for the REST endpoint

Example(s):

1
var r = new RESTMessageScripted("get", "https://myinstance.service-now.com/incident.do");
1
var r = new RESTMessageScripted("post", "https://login.salesforce.com/services/oauth2/token");

addheader( name, value )
Adds an HTTP Header name/value pair to the request. The system will issue these headers in the order that they were added.
name: a string representation of the header name
value: a string representation of the header value

Example(s):

1
2
3
var r = new RESTMessageScripted("get", "https://myinstance.service-now.com/incident.do");
r.addHeader("Content-Type", "application/json");
r.addHeader("Content-Accept", "application/json");

addRequestParameter( name, value )
Adds an HTTP Request parameter to the request. The system will issue these request parameters in the order that they were added.
name: a string representation of the parameter name
value: a string representation of the parameter value (note: values will be URL Encoded automatically.)

Example(s):

1
2
3
var r = new RESTMessageScripted("get", "https://myinstance.service-now.com/incident.do");
r.addRequestParameter("JSONv2", "true");
r.addRequestParameter("sysparm_query", "active=1^priority=1");

setContent( content )
Sets the Request Body Content (Used for POST and PUT functions). Otherwise this is just ignored.
content: a string representation of the full body content of the POST or PUT request.

Example(s):

1
2
3
4
5
6
var r2 = new RESTMessageScripted("post", "https://myinstance.service-now.com/incident.do");
r2.addHeader("Content-Type", "application/json");
r2.addRequestParameter("JSONv2", "true");
r2.addRequestParameter("sysparm_action", "insert");
r2.setBasicAuth("admin", "myadminpassword");
r2.setContent('{"short_description":"Hello world 4 56", "cmdb_ci":"DatabaseServer2"}');

execute()
Executes the REST call. If the REST call is synchronous, it will return the traditional RESTResponse object that is currently returned by the OOB RESTMessage class. If the REST call is asynchronous (via MID Server), no response is returned.

Example(s):

1
2
3
4
5
6
7
var r = new RESTMessageScripted("get", "https://myinstance.service-now.com/incident.do");
r.addHeader("Content-Type", "application/json");
r.addRequestParameter("JSONv2", "true");
r.addRequestParameter("sysparm_query", "active=1^priority=1");
r.setBasicAuth("admin", "myadminpassword");
var response = r.execute();
gs.log(response.getBody());
1
2
3
4
5
6
7
8
var r2 = new RESTMessageScripted("post", "https://myinstance.service-now.com/incident.do");
r2.addHeader("Content-Type", "application/json");
r2.addRequestParameter("JSONv2", "true");
r2.addRequestParameter("sysparm_action", "insert");
r2.setBasicAuth("admin", "myadminpassword");
r2.setContent('{"short_description":"Hello world 4 56", "cmdb_ci":"DatabaseServer2"}');
r2.setMIDServer("MID2");
var response2 = r2.execute();

All other functions associated with with out-of-box RESTMessage library are still available. They include:

  • setBasicAuth( username, password )
  • setMIDServer( midServerName )
  • setRestEndPoint( endpointURL )

All responses and behaviors from this library are the same as those processed by the RESTMessage library. Please see the following two documents for more information:

Contribute on GitHub

I have created a respository on GitHub so that others can help contribute and fix any issues with the library. You can get access to the repository at: Scripted Rest Messages on GitHub.

Installation Instructions

This is a ServiceNow Update Set file in XML format. Simply upload it into your instance using the instructions on loading XML-based Update Sets into your Instance.

Once you have loaded, previewed, and committed the update set, you will be able to access the library in your scripts.

Download

Scriptable RESTMessage Library