Creating RITMs from multi-row variable set
Import
·
May 29, 2019
·
article
The code below should create RITMs from the mrvs. It should also copy columns from mrvsa to variables if they have the same prefix.
Business rule: after; on insert
Table: [sc_request]
(function executeRule(current, previous /*null when async*/) {
var rowCount1 = '';
var x = [];
var y = [];
var rowCount = '';
var catItemId = '';
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request',current.sys_id);
ritm.query();
while(ritm.next()) {
catItemId = ritm.cat_item;
//////////below code is checking whether is the variables set starting from "mrvsa_" ////
//gs.log("Test 1 " + ritm.number,'piotr');
var gr1 = new GlideRecord('sc_item_option_mtom');
gr1.addQuery('request_item', ritm.sys_id);
gr1.addQuery('request_item.request', current.request);
gr1.addQuery('sc_item_option.item_option_new.variable_set.internal_name','STARTSWITH','mrvsa_');
gr1.query();
//If we find a matching variable in another item update its value
while(gr1.next()){
rowCount1 = gr1.sc_item_option.item_option_new.variable_set.internal_name.toString();
x.push(gr1.sc_item_option.item_option_new.name.substring(2).toString());
}
//////////////////below code pushes to array "y" all variables starting from "s_"///////
var gr2 = new GlideRecord('sc_item_option_mtom');
gr2.addQuery('request_item', ritm.sys_id);
gr2.addQuery('request_item.request', current.request);
gr2.addQuery('sc_item_option.item_option_new.name','STARTSWITH','s_');
gr2.query();
//If we find a matching variable in another item update its value
while(gr2.next()){
y.push(gr2.sc_item_option.item_option_new.name.substring(2).toString());
}
rowCount = ritm.variables[rowCount1].getRowCount();
if (rowCount1) {
//gs.log("Obiekt " + ritm.variables.mvrs_affected_usr);
var row1 = ritm.variables[rowCount1];
for (var i = 1; i < rowCount; i++) {
var row = ritm.variables[rowCount1].getRow(i);
var affectedUser = row.mvr_affected_user;
var location = row.mvr_location;
var requestId = current.sys_id;
var helper = new GlideappCalculationHelper();
helper.addItemToExistingRequest(requestId, catItemId, 1);
var num = helper.rebalanceRequest(current.request);
}
var ritm1 = new GlideRecord('sc_req_item');
ritm1.addQuery('request',current.sys_id);
ritm1.query();
var k = 0;
while (ritm1.next()) {
if (ritm1.sys_id != ritm.sys_id) {
var rec = new GlideRecord('sc_item_option_mtom');
rec.addQuery('request_item', ritm.sys_id);
rec.query();
while(rec.next()){
//Query for the same variable associated with the parent Request's items
//gs.log("DXC1 "+ ritm.number + " "+ "REC " +rec.sc_item_option.item_option_new.name);
var itemVars = new GlideRecord('sc_item_option_mtom');
itemVars.addQuery('request_item', ritm1.sys_id);
itemVars.addQuery('request_item.request', current.request);
itemVars.addQuery('sc_item_option.item_option_new.name', rec.sc_item_option.item_option_new.name.toString());
itemVars.query();
//If we find a matching variable in another item update its value
while(itemVars.next()){
//Get the variable value record
var itemVar = new GlideRecord('sc_item_option');
itemVar.get(itemVars.sc_item_option);
itemVar.value = rec.sc_item_option.value.toString();
itemVar.update();
}
}
}
///////////////////below code copies variables from mvrs to the newly created RITMs
var uniqueArray1 = new ArrayUtil().unique(x);
var uniqueArray2 = new ArrayUtil().unique(y);
var b = '';
var c = '';
var d = 'affected_user';
for(var j = 0; j < uniqueArray2.length; j++) {
if (uniqueArray1.indexOf(uniqueArray2[j]) != -1 ) {
b = "s_" + uniqueArray2[j];
c = "m_" + uniqueArray2[j];
//gs.log("Test For" + b + " "+ c, 'piotr');
ritm1.variables[b] = ritm.variables[rowCount1][k][c];
if (d == y[j]) {
ritm1.u_affected_user = row1[k][c];
}
ritm1.update();
}
}
// gs.log('Test AA '+ ritm.number + " " + ritm1.number + " " +ritm.cat_item.name,'piotr');
// // There is a static copying approach below
// ritm1.variables.s_affected_user = row1[k].m_affected_user;
// // ritm1.u_affected_user = row1[k].m_affected_user;
// ritm1.variables.s_location = row1[k].m_location;
// ritm1.variables.s_manager = row1[k].m_manager;
k++; //This k++ iterates across all RITMs to copy variables from mrvs
//
}
}
}
})(current, previous);
View original source
https://www.servicenow.com/community/developer-articles/creating-ritms-from-multi-row-variable-set/ta-p/2330127