Example ServiceNow API Script

The following script is an example only, of how an integration between N-able N-central and ServiceNow using the Custom PSA API can be achieved. You can also creat another script that may be more suitable for your needs.

Password in Script

You will have to change the user and password (variables "user" and "password" in the function process) in the script below to your local ServiceNow account credentials.

The account you use in this script should have at least following roles assigned. Note that the list may not be accurate and there may be unnecessary roles and the list may change in future:

  • dependency_views
  • itil_admin
  • assessment_admin
  • sn_bm_client.benchmark_data_viewer
  • web_service_admin
  • pa_viewer
  • template_editor
  • rest_api_explorer
  • itil
  • personalize_dictionary
  • cmdb_query_builder
  • view_changer
  • api_analytics_read
  • certification
  • rest_api_explorer.

The script:

function writeResponseBody(writer, ticket_sys_id) {

writer.writeString('{');

writer.writeString('"externalTicketId": "' + ticket_sys_id + '",');

writer.writeString('"ticketUrl": "https://ven02310.service-now.com/incident.do?sys_id=' + ticket_sys_id + '"');

writer.writeString('}');

}

 

function buildRequestBody(payload, state) {

var json = new JSON();

var defaultCallerId = '6377043fdbaf530009a1a961ca9619b8';

 

var longDescription = json.encode(payload.details);

var title = payload.title;

var category = payload.customTags.category;

var subcategory = payload.customTags.subcategory;

var urgency = payload.customTags.urgency;

var impact = payload.customTags.impact;

 

if (!category)

category = 'Software';

if (!subcategory)

subcategory = 'Email';

if (!urgency)

urgency = '2';

if (!impact)

impact = '5';

 

var statusString = '"' + ['state', state].join('": "') + '"';

var descriptionString = '"' + ['description', longDescription].join('": ');

var caller_idString = '"' + ['caller_id', defaultCallerId].join('": "') + '"';

var categoryString = '"' + ['category', category].join('": "') + '"';

var subcategoryString = '"' + ['subcategory', subcategory].join('": "') + '"';

var urgencyString = '"' + ['urgency', urgency].join('": "') + '"';

var impactString = '"' + ['impact', impact].join('": "') + '"';

var shortDescriptionString = '"' + ['short_description', title].join('": "') + '"';

 

var bodyPrepare = [descriptionString, statusString, caller_idString, categoryString, subcategoryString, urgencyString, impactString, shortDescriptionString];

return '{' + bodyPrepare.join(', ') + '}';

}

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var user = 'user.name';

var password = 'Password';

 

var payload = request.body.data;

 

var internalRequest = new sn_ws.RESTMessageV2();

internalRequest.setBasicAuth(user, password);

internalRequest.setRequestHeader('Accept', 'application/json');

internalRequest.setRequestHeader('Content-Type', 'application/json');

 

var internalResponse;

var endpointBase = 'https://ven02310.service-now.com/api/now/table/incident';

 

if (JSUtil.isEmpty(payload))

return new sn_ws_err.BadRequestError('No payload has been provided!');

 

switch(payload.action) {

case 'CREATE':

internalRequest.setEndpoint(endpointBase);

internalRequest.setHttpMethod('POST');

internalRequest.setRequestBody(buildRequestBody(payload, 'New'));

internalResponse = internalRequest.execute();

break;

case 'RESOLVE':

internalRequest.setEndpoint(endpointBase + '/' + request.body.data.psaTicketNumber);

internalRequest.setHttpMethod('PUT');

internalRequest.setRequestBody(buildRequestBody(payload, 'Closed'));

internalResponse = internalRequest.execute();

break;

case 'UPDATE':

internalRequest.setEndpoint(endpointBase + '/' + request.body.data.psaTicketNumber);

internalRequest.setHttpMethod('PUT');

internalRequest.setRequestBody(buildRequestBody(payload, 'New'));

internalResponse = internalRequest.execute();

break;

case 'REOPEN':

internalRequest.setEndpoint(endpointBase + '/' + request.body.data.psaTicketNumber);

internalRequest.setHttpMethod('PUT');

internalRequest.setRequestBody(buildRequestBody(payload, 'New'));

internalResponse = internalRequest.execute();

break;

default:

return new sn_ws_err.BadRequestError('No action has been provided or the action is unknown!');

}

 

var json = new JSON();

var parser = new JSONParser();

var parsed = null;

var writer = null;

var headers = null;

 

parsed = parser.parse(internalResponse.getBody());

 

writer = response.getStreamWriter();

headers = {};

headers['Content-Type'] = 'application/json';

response.setStatus(200);

response.setHeaders(headers);

writeResponseBody(writer, parsed.result.sys_id);

})(request, response);