The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Populating a Variable on sc_req_item table on update of catalog variable

alexcharleswort
Tera Expert

I have a need to be able to populate a variable located on the request item table (u_tra_amnt) when a catalog variable (tra_amnt_given) is updated from the task level. I know I need a business rule and I have been able to run a couple and successfully populate that variable on submit, but if any changes are made to that variable, it doesn't update. I am using after update functionality, but it's not working.

The script I am using is current.u_tra_amnt = current.variables.tra_amnt_given;

That's a BR on the sc_req_table

Any thoughts??

Thank you all in advance!

17 REPLIES 17

drjohnchun
Tera Guru

Hi Alex - you can't dot walk into variables, so your "current.variables.tra_amnt_given" wouldn't work. Below is a reply I provided for another post that helped resolve the issue.



Using GlideRecord to Query Tables 3.6   Querying Service Catalog Tables reads:



You cannot directly query the variables of the Service Catalog Request Item table [sc_req_item]. Instead, query the Variable Ownership table [sc_item_option_mtom] by adding two queries, one for the variable name and another for the value. The query returns the many-to-many relationship, which you can dot-walk to the requested item. The following example finds the request items that have the variable named 'item_name' with a value of 'item_value' and displays the request item numbers:



and provides this example:



var gr = new GlideRecord('sc_item_option_mtom');
gr.addQuery('sc_item_option.item_option_new.name','item_name');
gr.addQuery('sc_item_option.value','item_value');
gr.query();

while (gr.next()) {
      gs.addInfoMessage(gr.request_item.number);
}



Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

Winner of November 2016 Members' Choice Award


Alright, so I think I see what you are saying in this but I might need a little more hand holding as I want it to populate a variable and not just give me the item numbers that have this variable.



So... if I were to make a business rule....



Table: sc_req_item


when: after


update: true



script:


var gr = new GlideRecord('sc_item_option_mtom');
gr.addQuery('sc_item_option.item_option_new.name','tra_amnt_given');
gr.addQuery('sc_item_option.value','tra_amnt_given');
gr.query();


while (gr.next()) {
      ??????????????????????????;
}


Here's what can be done first to make sure the query is working. Can you run this in Background Script and tell me what the output is? Use the sys_id of a sc_req_item record that you want to test with and also set the item_name and item_value variables to the correct values before running it:



var sys_id = current.sys_id, item_name = 'tra_amnt_given', item_value = '???';   // set correct values here
var gr = new GlideRecord('sc_item_option_mtom');
gr.addQuery('request_item',sys_id);
gr.addQuery('sc_item_option.item_option_new.name',item_name);
gr.addQuery('sc_item_option.value',item_value);
gr.query();


gs.info(gr.getRowCount());


it says there is an error at line one. I assume I am supposed to just put a test value in there for an item that I have already entered?