Friday, 30 November 2018

Scripted REST API to create Incident in ServiceNow using Google Assistant

Scripted REST API 

(function process(/*REST API Request*/ request, /*REST API Response*/ response){

var state,assignTo,short_desc,nmbr,description,urgency,impact,priority,assignGroup,message;

/***************Recieve data from Google assistant for Incident***************/

var resWriter=response.getStreamWriter();
var headers={};
var responseobj = {};
var requestBody = request.body;
var requestobj = requestBody.data;
var displayName=requestobj.queryResult.intent.displayName;
var incident_number = requestobj.queryResult.parameters.incident_number;
var city_name = requestobj.queryResult.parameters.geo_city;
var company = requestobj.queryResult.parameters.companies;
var building = requestobj.queryResult.parameters.buildings;


short_desc = requestobj.queryResult.parameters.short_description;
var desc = requestobj.queryResult.parameters.description;
description=short_desc+desc;

/************************* Recieve Impact for Incident****************************************/

var imp=requestobj.queryResult.parameters.impact;
if(imp==1||"High" || "Hi"){
impact=imp;
}
if(imp==2||"medium"){
impact=imp;
}
if(imp==3||"low"){
impact=imp;
}
/************************* Recieve Urgency for Incident*************************************************/

var urg=requestobj.queryResult.parameters.urgency;
if(urg==1||"High" || "Hi"){
urgency=urg;
}
if(urg==2||"medium"){
urgency=urg;
}
if(urg==3||"low"){
urgency=urg;
}

/**************************Create Incident in ServiceNow as requested *****************************/

if(displayName == 'create_incident'){
var gr1 = new GlideRecord('incident');
gr1.initialize();
gr1.short_description=short_desc;
gr1.impact=impact;
gr1.urgency=urgency;
gr1.description=description;
gr1.assignment_group='<sys_Id  of Grooup>';
gr1.insert();
nmbr=gr1.number;
state=gr1.getDisplayValue('state');
priority=gr1.getDisplayValue('priority');
message="Great! Your incident is created with incident number "+ nmbr+", with priority "+priority+", and it's currently in "+state+" state. Please note down the incident number for reference. Thank you!";
}



/*****************Send response back to Ok Google **********************/


headers['Content-Type']='application/json';
response.setStatus(200);
response.setHeaders(headers);
var response_body={
"fulfillmentText": message ,
"payload": {
"google": {
"expectUserResponse": true,
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": message
}
}
]
}
}
}
};
resWriter.writeString(global.JSON.stringify(response_body));



})(request, response);

No comments:

Post a comment