Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to Add Variables to Old RITMs

kshaw
Giga Guru

I have a variable recently added to my catalog item. I want to now add that variable to old RITMs so that I can make use of it in reports on all records of this requested item.

Variable is defined as:

  • name 
  • Question
  • sys_id
  • order
  • part of a variable set

I want to add the variable to old RITMs so that it shows up in the correct variable set at the correct order (for correct display on form).

 

It may be a 2 step process:

  • add variable to existing RITMs (if missing)
  • set the value of the newly added variable per RITM (table lookup using another variable value)
6 REPLIES 6

Rhodri
Tera Guru

You'll want a background script that you can run and update the two below tables:

 

sc_item_option - the variable data

sc_item_option_mtom - the relationship between the variable data and the request item

 

 

 

//Populate historical variables
//variable ownership table sc_item_option_mtom
//variable data table sc_item_option

var stringQuery = ""; //Replace with your own encoded query to search for just the request items you want to update
var ritmGlide = new GlideRecord('sc_req_item');
ritmGlide.addEncodedQuery(stringQuery);
ritmGlide.query();
while(ritmGlide.next()){
    //create a variable
    var itemOptionGlide = new GlideRecord('sc_item_option');
    itemOptionGlide.initialize();
    itemOptionGlide.value = 'value_you_need_to_set'; //you could use the ritm you are currently gliding to, like ritmGlide.variables.another_variable_name
    itemOptionGlide.item_option_new = 'b04922da1b5e59106bd037b5464bcb0a'; //SysID of the question from the cat item
    itemOptionGlide.order = 5; //Or whatever order you need it to be, to display correctly
    itemOptionGlide.insert();

    //Create variable ownership
    var mtomGlide = new GlideRecord('sc_item_option_mtom');
    mtomGlide.initialize();
    mtomGlide.request_item = ritmGlide.sys_id; //Current RITM we are making variables for
    mtomGlide.sc_item_option = itemOptionGlide.sys_id; //sys id of the variable we made before
    mtomGlide.insert();
}

 

 

 

Rhodri_0-1664929433013.png

 

 

From messing around in the past, I think you don't even need to have the variable in the catalog item (obviously you do because it's a new variable you've added to the item), but you can just insert a value in any variable and relate it to the request item.

 

But you are correct, basically adding a new variable to a catalog item doesn't create these records in these two tables for historical requests from that cat item, so you'd need to do it via script.

@kshaw Just to let you know I ended up using this script for it's actual purpose today, rather than just in a testing scenario and worked well for me, so hopefully if you are still in need of it, it might be some use!.

 

Rhodri