Backsetting a variable from a new variable set on existing request items via script?

pfeifn
Kilo Expert

Is it possible to add a variable to old requested items once a new variable set has been assigned to a catalog item?

For example, let's say we have an Item "Application Access" and we add a new variable set with a variable called "request_type" I'd like to set all of the existing application access requests request_type values so they can be used for reporting, however when I query the sc_req_item rows, the "request_type" variable is not included on the variables object, and cannot be added.

Is there a way to add it via script? Thanks!

Just to be clear, since I did find a previous question with a not-accepted response stating to do so. I cannot just query the sc_req_item table and set the record.variables.[variable] and update. The [variable] is not on the record.variables object because it did not exist at the time of the record creation and the object does not allow new attributes to be added. I do not know whether this would work if the variable was not part of a variable set, this question is specifically about a variable attached to a variable set in which the variable set was added to the catalog item AFTER after the requests were created.

1 ACCEPTED SOLUTION

Kalaiarasan Pus
Giga Sage

sc_item_option_mtom stores the variable information for RITM. So you would have to write a script to insert the missing variables record into the table for the RITM's that you are looking for.

View solution in original post

4 REPLIES 4

sachin_namjoshi
Kilo Patron
Kilo Patron

You can run one time fix script to update variable ( as part of variable set) for existing sc_req_item records.

Please let me know if you need help with script.

 

Regards,

Sachin

if you just mean this, it does not work. It does not allow addition of a new variable to the variables object in the javascript itself. [variable_name] will come in as undefined and still be undefined after attempted assignment. ( I do not know if this is the same if it is a variable directly assigned to the catalog item, this is a catalog item to a variable set in which the variable set was assigned after the requested item were created )

var gr = new GlideRecord("sc_req_item");
gr.addQuery("cat_item","[blah]");
gr.query();
while(gr.next()){
    gr.variables.[variable_name]="value";
    gr.update();
}

Kalaiarasan Pus
Giga Sage

sc_item_option_mtom stores the variable information for RITM. So you would have to write a script to insert the missing variables record into the table for the RITM's that you are looking for.

This is mostly the correct answer for anyone checking in. We ended up going in a different direction as ServiceNow advised against doing this as it can cause issues with scripts, but it does work. Unfortunately for our purposes of being able to have a shared variable between two items on a report, the report's functions did not work correctly.

If you are JUST trying to back set variables though, here is a hackjob example. You need to set the variable in the dependent item table and then set it in that join table listed above.

//The Dependent Table
var tab="sc_item_option";
var item_option_new="c1406b150fb3024047e27d4ce1050e0c";
var value="Application Access";

//The Join Table
var tab2="sc_item_option_mtom";

//Grab Request Item(s)
var gr = new GlideRecord("sc_req_item");
gr.addQuery("cat_item","85b693f90f853100b42a05cce1050eaa");
gr.orderBy("sys_created_on");
gr.setLimit(1);
gr.query();
if(gr.next()){

gs.print(gr.sys_id);
gs.print(gr.variables.request_type);
//check that variable does not exist yet
if(typeof gr.variables.request_type == "undefined"){

//create the dependent value record
var g = new GlideRecord(tab);
g.item_option_new = item_option_new;
g.value=value;
var out = g.insert();
gs.print(out);

//create the join to the RITM
var g2 = new GlideRecord(tab2);
g2.request_item=gr.sys_id + "";
g2.sc_item_option = out + "";
g2.insert();

}

}