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.

Query RITM variables and print out Questions instead of Variable Names.

nowdev
Kilo Contributor

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);
}
1 ACCEPTED SOLUTION

SanjivMeher
Mega Patron
Mega Patron

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.

View solution in original post

4 REPLIES 4

SanjivMeher
Mega Patron
Mega Patron

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.

Thanks! That worked out perfectly!

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

userpoth1
Tera Contributor

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