How to pass RITM variable values inatead of SC task varioable value.

VIKAS MISHRA
Tera Contributor

we have completed one integration where from servicenow we are sending the SC_task's variables value to third party tool, but this integration has been done through rest API and the issue is coming that thorugh BR we are sending variables value but when we are sending it thorugh task vriables value then there is some delay in reaosn meanwhile task table does not get the variables value hence i want to send the values from RITM values instead of SC_ task.

 

Below is the small part of code of BR through which the task variables value are passig and i want to make some changes here so that it will be  passikng the variables from RITM not from SC task .

 

var obj ={};
for (var i in current.variables) {
gs.log("Variable" + current.variables);
if (current.variables.hasOwnProperty(i)) {
gs.log("Inside If Variable");
var variable = current.variables[i];
gs.log("Variables--> " + i + ':' + variable);
obj[i] = variable.toString();
}
}

 

 

1 ACCEPTED SOLUTION

Is it ok if we change the table to RITM? Because doing so, you can use the same script.

Otherwise, you can use the sc_task table, but need to glide to RITM table and script like this:

var grRITM= new GlideRecord('sc_req_item');
grRITM.addQuery('sys_id',current.request_item);
grRITM.query();
if(grRITM.next()){

var obj ={};
for (var i in grRITM.variables) {
gs.log("Variable" + grRITM.variables);
if (grRITM.variables.hasOwnProperty(i)) {
gs.log("Inside If Variable");
var variable = grRITM.variables[i];
gs.log("Variables--> " + i + ':' + variable);
obj[i] = variable.toString();
}
}
}

 

You can mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards

View solution in original post

8 REPLIES 8

mdash
Giga Guru

Hi Vikas,

The BR is running on which table? Is it sc_task?


Yes running on SC_task table, after update and insert

Is it ok if we change the table to RITM? Because doing so, you can use the same script.

Otherwise, you can use the sc_task table, but need to glide to RITM table and script like this:

var grRITM= new GlideRecord('sc_req_item');
grRITM.addQuery('sys_id',current.request_item);
grRITM.query();
if(grRITM.next()){

var obj ={};
for (var i in grRITM.variables) {
gs.log("Variable" + grRITM.variables);
if (grRITM.variables.hasOwnProperty(i)) {
gs.log("Inside If Variable");
var variable = grRITM.variables[i];
gs.log("Variables--> " + i + ':' + variable);
obj[i] = variable.toString();
}
}
}

 

You can mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards

Below is my whole BR script which is created on SC task table,

 

Could you nopw please check this script and let me know exactly where i need to make the changes so that varioables will go from RITM not from the Task .

 

******************************************

(function executeRule(current, previous /*null when async*/ ) {
if (current.u_external_ticket_number == '') {
try {
var jsonBody = {};
// var varjsonBody = {};
var rInsert = new sn_ws.RESTMessageV2('Ultima Request', 'Create');
var descbody = current.description;
descbody = descbody.replace(/(\r\n|\n|\r|\\n)/gm, "");
// descbody = JSON.stringify(descbody);
descbody = descbody.replace(/"/g, "'");
descbody = descbody.replace(/\\/g, "\\\\");

var shortDesc = current.short_description;
shortDesc = shortDesc.replace(/(\r\n|\n|\r|\\n)/gm, "");
// shortDesc = JSON.stringify(shortDesc);
shortDesc = shortDesc.replace(/"/g, "'");
shortDesc = shortDesc.replace(/\\/g, "\\\\");

//rInsert.setRequestBody({"short_description":"test"});
gs.log("Description : " + descbody + "-----" + shortDesc);

jsonBody.short_description = shortDesc;
jsonBody.description = descbody;
jsonBody.caller_id = 'Costa_API_user';
jsonBody.priority = current.getValue('priority');
jsonBody.state = 1;
jsonBody.u_customer_reference = current.getValue('number');
jsonBody.u_task_type = 'Request';

// rInsert.setStringParameterNoEscape('short_description', shortDesc);
// rInsert.setStringParameterNoEscape('description', descbody);

// rInsert.setStringParameterNoEscape('caller_id', 'Costa_API_user');
// rInsert.setStringParameterNoEscape('priority', current.priority);

// rInsert.setStringParameterNoEscape('state', 1);

// rInsert.setStringParameterNoEscape('u_customer_reference', current.number);
// rInsert.setStringParameterNoEscape('u_task_type', 'Request');
var obj ={};
for (var i in current.variables) {
gs.log("Variable" + current.variables);
if (current.variables.hasOwnProperty(i)) {
gs.log("Inside If Variable");
var variable = current.variables[i];
gs.log("Variables--> " + i + ':' + variable);
obj[i] = variable.toString();
}
}


jsonBody.u_variables = obj;
// rInsert.setStringParameterNoEscape('u_variables', variable);
gs.log("JSON VALUE --> " + JSON.stringify(jsonBody));

rInsert.setRequestBody(JSON.stringify(jsonBody));
var insertResponse = rInsert.execute();
var insertResponseBody = insertResponse.getBody();
var insertHttpStatus = insertResponse.getStatusCode();
var parsedData = JSON.parse(insertResponseBody);
gs.log(JSON.stringify(parsedData) + " Ultima Request Task creation response : " + insertResponseBody, 'reqUltimaCreate');
current.u_external_ticket_number = parsedData.result.u_external_ticket;
current.u_status = parsedData.result.u_status;
current.state = '-5';
current.update();

} catch (ex) {
var message = ex.message;
}
} else {
try {
gs.log("Inside Update Section ");

var rUpdate = new sn_ws.RESTMessageV2('Ultima Request', 'update');
//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);
var req = '{"u_customer_reference":"' + current.number + '"';
if (current.priority.changes())
req = req + ',"priority":"' + current.priority + '"';

if (current.work_notes.changes()) {
var workNotes = current.work_notes.getJournalEntry(1);
gs.log("Work Notes:-"+workNotes);
workNotes = workNotes.replace(/(\r\n|\n|\r|\\n)/gm, "");
workNotes = JSON.stringify(workNotes);
workNotes = workNotes.replace(/"/g, "'");
workNotes = workNotes.replace(/\\/g, "\\\\");
req = req + ',"work_notes":"' + workNotes + '"';
}

if (current.assignment_group.changes()) {
if (current.assignment_group == 'e8c3483c1b118510f48c65f7b04bcb0d') { // Ultima support group
req = req + ',"assignment_group":"' + 'd934315d1b4a2300ab0887fe6e4bcbf5' + '"'; // ultima servicedesk 24*7
}

}
if (current.state.changes()) {
if (current.state.changesTo('152')) { // Assess
req = req + ',"work_notes":"' + 'State is Assess' + '"';
} else if (current.state.changesTo('154')) { //In Progress
req = req + ',"state":"' + '6' + '"';

} else if (current.state.changesTo('157')) { // closed
var closeNotes = current.close_notes;
closeNotes = closeNotes.replace(/(\r\n|\n|\r|\\n)/gm, "");
closeNotes = JSON.stringify(closeNotes);
closeNotes = closeNotes.replace(/"/g, "'");
closeNotes = closeNotes.replace(/\\/g, "\\\\");


req = req + ',"state":"' + '10' + '","work_notes":"' + current.close_code + '","close_notes":"' + closeNotes + '"';
} else {
gs.log(current.state + " state update is not in scope", 'ultimaReqUpdate');
}
}

req = req + '}';
gs.log("Update req body " + req, 'ultimaReqUpdate');
rUpdate.setStringParameterNoEscape('request', req);

var updateResponse = rUpdate.execute();
var updateResponseBody = updateResponse.getBody();
var updateHttpStatus = updateResponse.getStatusCode();
gs.log(" Ultima RequestTAsk update response : " + updateResponseBody, 'ultimaReqUpdate');
} catch (ex) {
var message1 = ex.message;
}
}

})(current, previous);