ServiceNow Integration

Integration Options

Integration with ServiceNow is possible in two directions

  • ServiceNow calling a Readibots REST endpoint

  • Readibots calling a ServiceNow REST API endpoint

ServiceNow → Calling Readibots

There are many ways for ServiceNow to make outbound REST calls to readibots. Below is only one example of many. Mileage may vary depending on the specific flow or situation at hand in ServiceNow. The code provided is for demonstration purposes only and should not be considered fit for purpose for any specific situation. We recommend working directly with your ServiceNow SME/consultant to ensure proper implementation.

 

STEP 1: Install the Readibots Class into ServiceNow

In order to facilitate workflows to deploy Readibots the Readibot class needs to first be created in the ServiceNow tenant which will then be available to anything needing an automation.

  1. Go to System Definition > Script Includes
    (available at https://YOURTENANT.service-now.com/nav_to.do?uri=%2Fsys_script_include_list.do ).

  2. Click the [New] button.

  3. Provide

    1. Name: Readibot

    2. Application: Global

    3. Accessible from: All application scopes

    4. Active: checked

  4. Paste the following class definition Glide script into the Script editor

    var Readibot = Class.create(); Readibot.prototype = { enableLogging: true, baseBotUrl: 'https://cloudbridge.ucclearly.com/storageapi/api/ExecuteScript/', callMethod: 'POST', dataFormat: 'application/json', initialize: function() { //empty initializer }, deploy: function deploy(botId,botToken) { //automatically uses the current sys_id and number var dataObj = { sysid: current.sys_id.toString(), number: current.number.toString() }; this.deployWithObject(botId,botToken,dataObj); }, deployWithNumber: function deployWithNumber(botId,botToken,recordNumber) { //automatically uses the current sys_id and number var dataObj = { number: recordNumber }; this.deployWithObject(botId,botToken,dataObj); }, deployWithSysId: function deployWithSysId(botId,botToken,sysId) { //automatically uses the current sys_id and number var dataObj = { sysId: sysId.toString() }; this.deployWithObject(botId,botToken,dataObj); }, deployWithObject: function deployWithObject(botId,botToken,dataObject) { //NOTE: the properties of dataObject can onlybe simple types; // complex types will send as empty string //force property values to be strings (work around weaknesses in JSON.stringify() function) var newObject={}; for (prop in dataObject) { newObject[prop] = dataObject[prop].toString(); } this.deployWithJson(botId,botToken,JSON.stringify(newObject)); }, deployWithJson: function deployWithJson(botId,botToken,jsonString) { var request; var responseBody; var responseStatusCode; var response; //this log message string may not always work depending on caller type/context this.log('Readibot '+botId+' deployed for '+current.number+' ['+current.sys_id+']'); try { request = new sn_ws.RESTMessageV2(); request.setEndpoint(this.baseBotUrl+botId); request.setHttpMethod(this.callMethod); request.setRequestHeader('Content-type',this.dataFormat); request.setRequestHeader('rb-ReadibotsToken',botToken); request.setRequestBody(jsonString); response = request.execute(); responseStatusCode = response.getStatusCode(); responseBody = response.haveError() ? response.getErrorMessage() : JSON.parse(response.getBody()); } catch(ex) { responseBody = ex.getMessage(); responseStatusCode = 500; } //this log message string may not always work depending on caller type/context this.log('Readibot for '+current.number+' ['+current.sys_id+'] returned: '+responseBody); }, log: function log(msg) { if (this.enableLogging) { var timestamp = new GlideDateTime().getNumericValue(); gs.log(msg, 'Readibots'); } }, type: 'Readibot' };
  5. Click the [Submit] button to save the Readibot class

STEP 2: Define a Bot or a Queue in Readibots

ServiceNow will need to call the REST endpoint of either a Bot or a Queue. The best approach is to make a POST request with a JSON payload. This is a default for a Queue, however, if using a Bot, the Bot must be set up to receive a JSON payload over REST. To do this ensure that the Bot has only one param of type [PsObject] (the name of the param can be anything).

 

In the Bot or Queue go to the Permissions tab and check the box under REST Access → Allow this bot to be executed via REST.

While this will provide a URL that can be called but GET, including a template for any parameter substitutions, it is recommended that a POST + JSON request be used. Use the Command Builder → REST → Execute, with Powershell & POST+JSON in order to get the https URL and token value you need.

Clicking the [Copy command] button will copy the ServiceNow Glide script into your copy buffer for use in the next step.

STEP 3: Calling the Bot from a Workflow

Bot deployment in ServiceNow is typically initiated from within a workflow; this is the model for this example.

  1. In a ServiceNow Workflow, create a Run Script node at the appropriate point where automation is to occur.

  2. Add the previously copied Glide script to the Run Script node. The copied script will look something similar to:

    // Requires the Readibot class script include in ServiceNow. For more information see // https://ucclearly.atlassian.net/wiki/spaces/CK/pages/1097564161/ServiceNow+Integration var botId = '123456a7-8b9c-09de-8f76-e54321d01234'; var botToken = '1234567890987654321AbCdE12fG3h4I5jK'; (new Readibot()).deploy(botId,botToken);


    Resulting in the following script node:

     

The Readibot class also provides a deployWithObject method which additionally accepts an specific object to send. This allows for any arbitrary object to be sent to the bot for automation. It is important to note that the property values within the object can only be intrinsic base types like string, number, boolean, etc. Complex values will end up being sent as empty strings.

Eg.
var botId = '123456a7-8b9c-09de-8f76-e54321d01234'; var botToken = '1234567890987654321AbCdE12fG3h4I5jK';
var dataObject = {
name : 'John',
id : 123456,
fullTime : true
};
(new Readibot()).deployWithObject(botId,botToken,dataObject);

ServiceNow will now call the bot when the Workflow is invoked and the Run Script node is activated. This example uses the “.deploy” method which will automatically send current.sys_id and current.number to the bot as

{ "sysid":"1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d" "number":"RITM0000001" }

From within the example bot above these values can be referenced by:

 

Readibots → Calling ServiceNow

Readibots can call ServiceNow by utilizing the ServiceNow module .

NOTE: The version of the ServiceNow module that cloud based bots will use is v1.8. The examples below are based on v3.2.0 which is not version compatible with v1.8.

It is recommended that the latest version of the module be installed on an On-Prem Proxy and have bots run on the proxy.

  1. Create a ConnectId of type ServiceNow. For the URL field only put in the FQDN (omit https: and all '/' characters). Set the Execute On to an On-Prem Proxy.

  2. Set the ConnectId as “Run As” for the script that will connect to ServiceNow (the ConnectId can be set as available for the script, in which case it can be referenced through $ConnectId['name of ConnectId'] )

  3. Initialize the ServiceNow session in the Readibots script

  4. Invoke the needed automation. Eg.

    Get a RequestedItem (RITM)


    Update / Close a RequestedItem (RITM)