- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2018 11:10 PM
Hello,
I was attempting to collect all variables' questions from a RITM, with the following script. However what I printed out was the variables' names, not the questions. For example, it printed out data_center (variable name), instead of Data Center (question). Any idea on how to modify the code? Thanks very much in advance!
var item = new GlideRecord('sc_req_item');
item.addQuery('number', 'RITM012301');
item.query();
var s =' ';
if(item.next()){
for(var variable in item.variable_pool) {
var answer = eval ("item.variable_pool." + variable + ".getDisplayValue()");
s+=variable+" = "+answer + " | "
}
gs.log(s);
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2018 11:15 PM
Use below script
var s='';
var item = new GlideRecord('sc_req_item');
item.addQuery('number', 'RITM012301');
item.query();
if(item.next()){
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(item.sys_id);// requested item sys_id
set.load();
var vs = set.getFlatQuestions();
for (var i=0; i < vs.size(); i++) {
if(vs.get(i).getLabel() != '' && JSUtil.notNil(vs.get(i).getDisplayValue()) ) {
s+= vs.get(i).getLabel() + ": " + vs.get(i).getDisplayValue() + "\n";
}
}
gs.log(s);
}
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2018 11:15 PM
Use below script
var s='';
var item = new GlideRecord('sc_req_item');
item.addQuery('number', 'RITM012301');
item.query();
if(item.next()){
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(item.sys_id);// requested item sys_id
set.load();
var vs = set.getFlatQuestions();
for (var i=0; i < vs.size(); i++) {
if(vs.get(i).getLabel() != '' && JSUtil.notNil(vs.get(i).getDisplayValue()) ) {
s+= vs.get(i).getLabel() + ": " + vs.get(i).getDisplayValue() + "\n";
}
}
gs.log(s);
}
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2018 11:44 PM
Thanks! That worked out perfectly!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2019 04:28 AM
Hi Sanjiv,
I have tried running the same script to pull the variables from REQ and ICM as we have the variables there too. But it's not working on these tables. Do tou kow if 'GlideappVariablePoolQuestionSet' only works on catalog tables?
Thanks, Rohit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2020 10:59 AM
This will get a string with all the question and answer pairs for an RITM:
var ritm = new GlideRecord('sc_req_item');
ritm.get(/* Your RITM sys_id here */); // current is sysapproval_approver
var variable_names = Object.keys(ritm.variables); // Get all the keys in the RITM's variables object
var string = ''; // This will hold the RITM's questions and answers
for (var index = 0; index < variable_names.length - 1; ++ index) {
var variable = ritm.variables[variable_names[index]];
string += variable.getLabel() + " " + variable.getDisplayValue() + "<br>"; // Get the label and display value of each variable
}
// string holds your question answer pairs
