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

Hi Vikas,

From the code it looks like there is major dependency on the sc_task table for many fields. Although we can write the BR on sc_req_item table , we will have to dot walk/glide back to many fields.

So instead, you can keep your script intact, just need to tweak a bit like this. You can test it and let me know if anything is still missing.

(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 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();
}
}
}


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);

 

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

Community Alums
Not applicable

Hi, Aren't the variables same on both the ritm and sc_task?

The variables vale are same on the task and on the RITM also.

But here RITM is already created and having variables so i can send the varibales value from RITM which would go fine,. But till now we are sending tghe variables from SC task which creates issues the issue is that When task generates and gets the variable value populated in task before that our script runs and we are sending the data but meanwhile task does not have any variable value so the varaible values are going blank to thiird party tool.

 

This is the reason i am trying to send the variables value from RITM not from task.

Community Alums
Not applicable

Okay. in that case can you change the table on Business rule to sc_req_item and try?