This article is a supplement to the ServiceNow documentation. For full documentation please refer ServiceNow official website
Checkout our NEW Video Channel you can like and subscribe too!

Introduction

Here we will describe the ServiceNow platform setup for the VIP Mobile App Integration. The VIP Mobile App Integration integrates ServiceNow instance with the VIP User Mobile app to enable VIP users with the capability to raise incidents, update specific incidents with comments, retrieve list of incidents, helpdesk contact details and contact details of Engineer assigned to VIP users. There are specific validations in place such as: • While creating incidents the Summary, Caller id, Category and Subcategory cannot be blank. • While updating incident the Incident sys_id and comments cannot be blank. • While retrieving list of incidents the Caller id cannot be blank or inactive.

Scripted REST service - VIPMobileAppIntegrator

To achieve the VIP Mobile App integration a scripted REST service is configured which has 5 resources to achieve 5 different operations:

Create Incident

  • Name: createRecord
  • HTTP Method: POST
  • Resource path: /api/mghmi/vipmobileappintegrator/create
  • Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
	var req = request.body.data;	
	var message = new vipIntegrationUtils().createRecord(req);	
	response.setContentType('application/json');
	response.setBody({
		message: message
	});
	
})(request, response);

Update Incident

  • Name: updateRecord
  • HTTP method: PATCH
  • Resource path: /api/mghmi/vipmobileappintegrator/update
  • Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
	var req = request.body.data;	
	var message = new vipIntegrationUtils().updateRecord(req);	
	response.setContentType('application/json');
	response.setBody({
		message: message
	});

})(request, response);

Get Incidents by Caller id

  • Name: getIncidents
  • HTTP method: POST
  • Resource path: /api/mghmi/vipmobileappintegrator/getincidents
  • Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
	var req = request.body.data;	
	var message = new vipIntegrationUtils().getIncidentsByUser(req);
	if(message=='Error: Caller id cannot be blank'){
		response.setContentType('application/json');
		response.setBody({
			message: message
		});
	}
	else{
		response.setContentType('application/json');
		response.setBody({
			message:message
		});
	}

})(request, response);

Get VIP Engineer Details

  • Name: getVIPEngineer
  • HTTP method: GET
  • Resource path: /api/mghmi/vipmobileappintegrator/vipengineer
  • Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
	var message = new vipIntegrationUtils().getVIPUserEngineer();
	response.setContentType('application/json');
	response.setBody({
		message:message
	});


})(request, response);

Get Location Helpdesk Details

  • Name: getLocationHelpDeskNumber
  • HTTP method: GET
  • Resource path: /api/mghmi/vipmobileappintegrator/gethelpdesknumber
  • Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
	var message = new vipIntegrationUtils().getLocationalHDDetails();
	response.setContentType('application/json');
	response.setBody({
		message:message
	});

})(request, response);

Authentication Profile

A service account is configured at the ServiceNow end with “soap” role to authenticate the integration. The Details is as below:

  • User ID: vipintegration_user
  • Active: true
  • Web service access only: true
  • Password: XXXX
  • Parent Role: soap

Scripted Include – vipIntegrationUtils

A script include is configured to handle the code for the integration.

Name: vipIntegrationUtils

Script:

var vipIntegrationUtils = Class.create();
vipIntegrationUtils.prototype = {
	initialize: function() {
	},
	createRecord: function(request){
		var short_desc = request.short_description;
		var desc = request.description;
		var userJson = this._callerDetails(request.user_name);
		var caller = userJson.id;
		var email = userJson.email;
		var contact = userJson.contact;
		var dept = userJson.department;
		var location = userJson.location;
		var city = userJson.city;
		var add_ph = userJson.add_ph;
		//var srvc_class = request.u_service_classification; 
		var category = request.category;
		var subcategory = request.subcategory;
		var s_id='';
		var data='';
		gs.info("***VIP Mobile App::Received parameters::Short Description:"+short_desc+", Description:"+desc+", VIP User:"+request.user_name+", Category:"+category+", Subcategory:"+subcategory);
		if(short_desc!="" && caller!="" && category!="" && subcategory!=""){
			var incGr = new GlideRecord('incident');
			incGr.initialize();
			incGr.caller = caller;
			incGr.sys_created_by = request.user_name;
			incGr.short_description = short_desc;
			incGr.description = desc;
			incGr.u_email = email;
			incGr.u_phone_number = contact;
			incGr.department = dept;
			incGr.location = location;
			incGr.city = city;
			incGr.u_additional_phone_number = add_ph;
			incGr.contact_type = "self-service";
			incGr.impact = 1;
			incGr.urgency = 1;
			incGr.u_service_classification = "Infrastructure Services";
			incGr.category = category;
			incGr.subcategory = subcategory;
			s_id = incGr.insert();
			data = s_id;
			gs.info("***VIP Mobile App::Incident created successfully with sys_id:"+data);
		}
		else{
			gs.error("***VIP Mobile App::Error: Summary or Caller or Service Classification or Category or Subcategory cannot be empty");
			data = "Error: Summary or Caller or Service Classification or Category or Subcategory cannot be empty";
		}
		return data;
	},
	
	updateRecord: function(request){
		var comment = request.comments;
		var id = request.sys_id;
		var data = '';
		if(comment!="" && id!=""){
			var incGr = new GlideRecord('incident');
			incGr.addQuery('sys_id',id);
			incGr.query();
			if(incGr.next()){
				incGr.comments = comment;
				incGr.update();
			}
			data =  "Update Successful";
			gs.info("***VIP Mobile App::Incident updated successfully with comments:"+comment);
		}
		else{
			gs.error("***VIP Mobile App::Error: Comment or Record id cannot be empty");
			data = "Error: Comment or Record id cannot be empty";
		}
		return data;
	},
	
	getIncidentsByUser: function(request){
		var user = request.user_name;
		var usrGr = new GlideRecord("sys_user");
		usrGr.get("user_name",user);
		var data = '';
		if(usrGr.active=='true'){
			var incJson = [];
			var jsonStr = {};
				var json = new JSON();
				var incGr = new GlideRecord('incident');
				incGr.addEncodedQuery('caller_id='+usrGr.sys_id+'^stateNOT IN6,8,7');
				incGr.query();
				while(incGr.next()){
					jsonStr.number = incGr.number.toString();
					jsonStr.user_name = incGr.caller_id.user_name.toString();
					jsonStr.short_description = incGr.short_description.toString();
					jsonStr.state = incGr.state.getDisplayValue();
					incJson.push(jsonStr);
				}
				data = json.encode(incJson);
			
			}
			else{
				gs.error("***VIP Mobile App::Error: Caller id is either blank or active user is not found in user database");
				data = "Error: Caller id is either blank or active user is not found in user database";
			}
			return data;
			
		},
		
		getVIPUserEngineer : function(){
			var incJson = [];
			var jsonStr = {};
				var json = new JSON();
				var data = '';
				var vipGr = new GlideRecord('u_vip_users_engineer');
				vipGr.query();
				while(vipGr.next()){
					jsonStr.u_vip_user_name = vipGr.u_vip_user_name.toString();
					jsonStr.u_engineer = vipGr.u_engineer.user_name.toString();
					jsonStr.u_engineer_business_phone = vipGr.u_engineer_business_phone.toString();
					incJson.push(jsonStr);
				}
				data = json.encode(incJson);
				return data;
			},
			
			getLocationalHDDetails : function(){
				var incJson = [];
				var jsonStr = {};
					var json = new JSON();
					var data = '';
					var locGr = new GlideRecord('u_locational_helpdesk');
					locGr.query();
					while(locGr.next()){
						jsonStr.u_location_name = locGr.u_location_name.toString();
						jsonStr.u_location_address = locGr.u_location_address.toString();
						jsonStr.u_helpdesk_contact = locGr.u_helpdesk_contact.toString();
						incJson.push(jsonStr);
					}
					data = json.encode(incJson);
					return data;
				},
				
				_callerDetails : function(user){
					var usr = {};
						var gr = new GlideRecord("sys_user");
						gr.addQuery('user_name',user);
						gr.query();
						if(gr.next()){
							usr.id = gr.sys_id.toString();
							usr.email = gr.email;
							usr.contact = gr.phone;
							usr.department = gr.department.toString();
							usr.location = gr.location.toString();
							usr.city = gr.location.city;
							usr.add_ph = gr.mobile_phone;
						}
						return usr;
					},
type: 'vipIntegrationUtils'
										};
    Content