RITM Variable reference table update changes

vinothkumar
Tera Guru

Catalog variable reference table update the old Request. Team, I have been working on changing the reference variable table change.

I have to write a script that will query the RITM that has this variable getting changed and query all the item and have to check if the variable value exist then iterate through item_option_table and update the RITM variable with the new table value.

Since we have more than 100 variable that is impacted by this, we have to query all this and do the logic to update the record.

Dont have any clue how can RITM be queried to iterate 100 variables names and if value exist, we have to update. 

 

 

 

My code that was drafted is below

 

var gr = new GlideRecord("sc_req_item");
gr.addEncodedQuery("Cat Item is One of in the excel sheet"); Having 100 catalog
gr.query();
while (gr.next())
{
var gr1 = new GlideRecord("item_option_new"); Having 100 Variables
gr1.addQuery("cat_item", gr.cat_item);
gr1.query();
while(gr1.next())
{
var gr2 = new GlideRecord("sc_req_item");
gr2.addQuery("sys_id", gr.sys_id);
gr2.query();
if(gr2.next())
{
// Write a logic, that will check for all the variables and if not empty, then do update with new reference record -- Please help me  to iterate the logic.

gr2.variables.gr1.name ! = ""
{

//Do the logic

}

}

}

4 REPLIES 4

AshishKM
Kilo Patron
Kilo Patron

Hi, 

Please give some example data for catalog/variables. Assuming all 100 catalogs have different variables ( out of 100 ) , so foe each catalog , we need to check for all 100 variable if they exist or not .. if exit then empty .. if empty then set the value...

need 2 array lists to iterate catalog -> variable -> setValue. Why arrayList : to simplify the iterating logic instead of adding more in encodedQuery. 

arrayCatalogItem

arrayVariableName

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Hello Ashish,

 

Below is I am trying, but still not working. When I simply print x[i] it is showing values.

 

Where as when I print gr.variables.x[i] it is not working as the way I am expecting. Can you help me

 

var x = ["a", "b", "c"];
var ugr = new GlideRecord('sc_req_item');
ugr.addEncodedQuery('cat_item=3a65ed971b6a70d0aba8b95bdc4bcb5a'); //
ugr.query();

while (ugr.next()) {

for(var i=0;i<x.length;i++)

{

if (!JSUtil.nil(ugr.variables.x[i])) {  

var req_item = ugr.sys_id; // CORRECT!
var current = new GlideRecord('sc_req_item');
current.get(req_item);


var ref_rec = new GlideRecord('xyz');
ref_rec.get(1.variables.x[i]);

var new_ref = new GlideRecord('FMC');
new_ref.addQuery('FCC', ref_rec.getDisplayValue());
new_ref.query();

 

if (new_ref.next())
{
current.variables.x[i] = new_ref.getUniqueValue();
current.update();
current.setWorkflow(false);
current.autosSysFields(false);
}
}

}
}

Ankur Bawiskar
Tera Patron
Tera Patron

@vinothkumar 

check if this link helps

https://community.servicenow.com/community?id=community_question&sys_id=f211ab511b521c90d2ccea89bd4b...

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hemanth14
Tera Contributor

 

Try this code

const catVar = [a,b,c,d];
const catItemSysId = "sys_id"; //Catalog item sys_id

var cItem = new GlideRecord('sc_req_item'); // Find all the RITMs for the catalog item
cItem.addQuery('cat_item', catItemSysId);
cItem.query();

while (cItem.next()) {
for(var i=0;i<catVar.length;i++) { // For each of the variables
var cItemVar = cItem.variables[catVar[i]];
if (!JSUtil.nil(cItemVar.getValue())) {
var tab = new GlideRecord('old_table');
tab.get(cItemVar.getValue());
if (tab.u_retrofit_action.getValue() == 'Variable on New table' ) { //If the tab record has been moved to New table
var assReg = tab.variable on New table.getRefRecord();
if (assReg.isValidRecord()) { // If there is a record available

cItemVar.setValue(assReg.sys_id) //Update the variable value to the sys_id of New record
cItem.update();
}
}
}
}
}