Solved: Im trying to print all variables into an email tem... - ServiceNow Community

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

Ryan Norton
Giga Contributor

this is what i have, but it only prints the variables in the preview notification. not the request item the email is pertaining to.

var gr = new GlideRecord("sc_req_item");

gr.addQuery(current);

gr.query();

if(gr.next()) {

  template.print(gr.cat_item.getDisplayValue() + "\n");

  template.print("\n");

  for (key in gr.variables) {

  var v = gr.variables[key];

  if(v.getGlideObject().getQuestion().getLabel() != '') {

  if(v.getDisplayValue() != ""){

  template.space(6);

  template.print('         ' +   v.getGlideObject().getQuestion().getLabel() + " : " + v.getDisplayValue() + "\n");

  }

        }

      }

}

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Ryan,



This code snippet is intended to run on approval records. Change the current.getValue('sysapproval') part to point to the right record sys_id as needed.



 


printVars();


function printVars(){


      var set = new GlideappVariablePoolQuestionSet();


      set.setRequestID(current.getValue('sysapproval'));


      set.load();


      var vs = set.getFlatQuestions();


      for (var i=0; i < vs.size(); i++) {


              if(vs.get(i).getLabel() != '') {


                      template.print('       ' +   vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");


              }


      }


}


View solution in original post

5 REPLIES 5

Chuck Tomasi
Tera Patron

Hi Ryan,



This code snippet is intended to run on approval records. Change the current.getValue('sysapproval') part to point to the right record sys_id as needed.



 


printVars();


function printVars(){


      var set = new GlideappVariablePoolQuestionSet();


      set.setRequestID(current.getValue('sysapproval'));


      set.load();


      var vs = set.getFlatQuestions();


      for (var i=0; i < vs.size(); i++) {


              if(vs.get(i).getLabel() != '') {


                      template.print('       ' +   vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");


              }


      }


}


Hey Chuck, I saw that one in another post, I'm running the notification on the sc_req_item table, When i try using that and replace sysapproval with current.request


or something alike, it doesn't display anything. I'm not quite sure the best way go about that part.


If you are running the notification sc_req_item and the variables are on that record, then use current.getValue('sys_id')



current.getValue('request') returns the sys_id of the parent request. That's why it didn't return anything - no variables there.


Thank you so much, it worked! This is end result script:



template.print("<p></p>Requested item: ");


printVars();


function printVars(){


      var set = new GlideappVariablePoolQuestionSet();


      set.setRequestID(current.getValue('sys_id'));


      set.load();


  template.print(current.getDisplayValue('cat_item') + "\n");


  template.print("\n");


      var vs = set.getFlatQuestions();


      for (var i=0; i < vs.size(); i++) {


              if(vs.get(i).getLabel() != '') {


  if (vs.get(i).getDisplayValue() != ''){


  template.space(6);


  template.print('       ' +   vs.get(i).getLabel() + " : " + vs.get(i).getDisplayValue() + "\n");


  }


              }


      }


}