ServiceNow Web Service : REST API and Javascript
Creating a RESTful web service in ServiceNow platform by using Javascript
Tools Used in this Tutorial
1. ServiceNow Instance
2. Javascript
Step 1: Creating a Business Rule
This tutorial assumes that you already have an accessible instance of ServiceNow that you can connect to. For more information on getting started with ServiceNow, visit their online tutorial.
Business Rule : MediumRESTAPI
Table :Incident
When to run: Short Description contains Medium.comScript
------var generateOutbound = new MediumRestAPIInterface(); generateOutbound.addRestInfo('post', current.sys_id);
Step 2: Creating a Script Include
In Script Include, we create to separate script include. The first one to communicate with the business rule and the second one to generate JSON.
Script Include : MediumRestAPIInterface
initialize: function() {
this.baseurl = "Http://medium.com";
this.jsonConverter = new MediumJSONConverter();
},addRestInfo: function(method, incidentNumber){
info = this.jsonConverter.incidentToJSON(incidentNumber);
this.endPointREST('MediumREST', info, incidentNumber, method);
},endPointREST: function(RESTEndpoint, info, sysId, methodRest){
var r = new RESTMessage(RESTEndpoint, methodRest);
r.setStringParameter('baseurl', this.baseurl);
r.setStringParameter('incident', sysId);
r.setXMLParameter('events', info);
var response = r.execute();},
Script Include : MediumJSONConverter
incidentToJSON: function(incidentNumber) {
if (JSUtil.nil(incidentNumber)) return; var gr = new GlideRecord("incident");
gr.addQuery('number', incidentNumber);
gr.query();
if (gr.next()) {
return this.incidentGRToJSON(gr);
}
return null;
},
incidentGRToJSON: function(pGR) {
var gr = pGR;
var lObj = new Object();
lObj.title = gr.short_description.getDisplayValue();
lObj.number = gr.number.getDisplayValue();
lObj.shortDescription = gr.short_description.getDisplayValue();
return new JSON().encode(lObj);
},
Step 3: Creating a REST Message
Navigate to System Web Services>Rest Message