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.

To Get all variables from catalog item

debo1
Kilo Contributor

How to get all the catalog item by using Business Rule or by using client script from Record Producer.

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

What is the Business Requirement?

 

You can get them via a report:

 

https://docs.servicenow.com/bundle/istanbul-performance-analytics-and-reporting/page/use/reporting/task/create-list-report-with-variable-columns-rows_RB.html

 

Or a script like below will output all variable values:

 

var grRequestedItem = new GlideRecord('sc_req_item');
grRequestedItem.setLimit(10);
grRequestedItem.query();
while (grRequestedItem.next()) {
 gs.addInfoMessage('Variables for ' + grRequestedItem.getDisplayValue());
  for (var prop in grRequestedItem.variables) {
    if (grRequestedItem.variables.hasOwnProperty(prop) ){
     var variable =  grRequestedItem.variables[prop];
      gs.addInfoMessage(prop + ':' + variable);
    }
  }
}

 Output

Variables for RITM0010006
acrobat:false
photoshop:false
Additional_software_requirements:
Variables for RITM0010007
new_email:jo.asd@asdas.com
Variables for RITM0010008
ergonomic_office:
Variables for RITM0010009
acrobat:true
photoshop:true
Additional_software_requirements:1

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

6 REPLIES 6

This script works great for variables directly on the catalog item, however, for variable sets it gets a bit messy. It seems to dump the entire variable set out in an invalid object ie if a variable doesn't have a value in the variable set no empty string is provided.

 

variable_set:question1_novalue:,question2_has_value:test,question3_novalue:,question4_novalue:

 

any suggestions on how to parse the variable set better? My end goal is to have a variable object with the ability to re-submit a similar request leveraging the cartJS api.

andrewdunn
Giga Expert

As Paablo indicated it depends on what you want to achieve - there have been quite a few articles on this throughout the forums:

 

Printing variable set in email notification

https://community.servicenow.com/community?id=community_question&sys_id=5c5b0721db9cdbc01dcaf3231f961904&view_source=searchResult

 

print variables to notes

https://community.servicenow.com/community?id=community_question&sys_id=6f1e03eddb9cdbc01dcaf3231f961964&view_source=searchResult

 

Im trying to print all variables into an email template using an email script

https://community.servicenow.com/community?id=community_question&sys_id=7df197eddbdcdbc01dcaf3231f961900&view_source=searchResult

 

The code that I use to print the variables (that have been selected) into the details of a ticket created via record producer is:

var test = []; 
for(var v in producer){  
  if (v.startsWith("IO") && producer[v] != 'false' && producer[v] != '' && producer[v] != 'No' && producer[v] != '-- None --') { //only variables 
    var question = new GlideRecord('item_option_new');  
   question.get(v.substring(2)); // Querying by sys_id to determine question text 
    test += question.question_text + ": " + producer[v].getDisplayValue() + "\n"; // Set key:value pair to variable 
  }  }
current.work_notes = test; // Set Work Notes on new record
current.description = test; // Set Detailed Description on new record

 

The code used to show the variables selected when sending an email for approval is a mail script

Used in the notification: ${mail_script:sc_request_approval}

Created in the Notification Email Script:

var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sysapproval);
 gr.query();
 while(gr.next()) {
 var stage = gr.stage.getDisplayValue();
 if (JSUtil.nil(stage))
 stage = gr.stage.getChoiceValue();
 template.print(gr.number + ": " + gr.cat_item.getDisplayValue() + "<br />");
  template.print("    Options Selected:<br />");
var keys = new Array();
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(gr.sys_id);
set.load();
var vs = set.getFlatQuestions();
for (var i=0; i < vs.size(); i++) {
if(vs.get(i).getLabel() != '') {
if(vs.get(i).getDisplayValue() != 'No' && vs.get(i).getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != '' && vs.get
(i).getDisplayValue() != "-- None --"){
template.space(4);
template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br />");

}}}}