How to fetch requested item variable via background scripts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2019 10:07 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2019 10:50 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2019 11:11 PM
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