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 fetch requested item variable via background scripts?

Servicenow10
Kilo Guru

hi,

 I need to fetch requested item variable's value from requested item table via script.please refer the image attached below.

i wrote a B R for that:

table : sc_item_option_mtom

script :

(function executeRule(current, previous /*null when async*/) {

// Add your code here
var item = new GlideRecord('sc_req_item');

item.addQuery("request", current.sys_id);

item.query();

if (item.next()) {

// grab all variable sets from the parent request

var vars = new GlideRecord('sc_item_option_mtom');

vars.addQuery('request_item', item.sys_id);

//var_own.addQuery('sc_item_option.item_option_new.u_approval_display', 'global');

var_own.orderBy('sc_item_option.item_option_new.order');

var_own.query();

while (var_own.next()){

gs.print(var_own.sc_item_option.item_option_new.question_text + ": " + eval('item.variable_pool.'+var_own.sc_item_option.item_option_new.name+'.getDisplayValue()') + "\n");

}

}

})(current, previous);

 

please check the script  and where I need to correct also what to write in when to run condition

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Use this script to get all the variables and it's values for particular RITM; run this in background script giving sys id of RITM

var grRequestedItem = new GlideRecord('sc_req_item');
grRequestedItem.addQuery('sys_id', '<ritmSysId>'); // give ritm sys id here
grRequestedItem.query();
while (grRequestedItem.next()) {
gs.print('Variables for ' + grRequestedItem.getDisplayValue());
for (var prop in grRequestedItem.variables) {
if (grRequestedItem.variables.hasOwnProperty(prop) ){
var variable = grRequestedItem.variables[prop];
gs.print(prop + ':' + variable);
}
}
}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

hi ankur,

 

still the same issue...actually i have one more B R on sc_task table

try {
var r = new sn_ws.RESTMessageV2('HTTP_REST_API', 'POST');
r.setStringParameterNoEscape('sys_id',current.sys_id);


var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.getMessage();
}

 

as you can see from the above code the whole output of sc task am getting into  my response but the variables is missing...please correct me