GlideappVariablePoolQuestionSet is showing hidden fields in notification

Ajai S Nair
Giga Guru

I have a notiifcation email script which uses GlideappVariablePoolQuestionSet to display the variables in the request item.Recently i have created a catalog item and while testing i found that hidden fields used as a counter variable are being displayed inside the notification.The script which is written in email script is :

var requestedItem = current.sys_id;

var set = new GlideappVariablePoolQuestionSet();

set.setRequestID(requestedItem);

set.load();

var vs = set.getFlatQuestions();

var count = 0;                                                          

                                           

while (vs.size() > count)

{

      if (vs.get(count).getLabel() != '' && vs.get(count).getLabel() != 'null' && vs.get(count).getLabel() != 'asfd' && vs.get(count).getDisplayValue() != '' && vs.get(count).getDisplayValue() != '-- None --' && vs.get(count).getDisplayValue() != ' ')

  {

  template.print("<tr><td style='border:none;border-bottom:solid #BFBFBF 1.0pt; padding:2.9pt 2.9pt 2.9pt 2.9pt'><p align=right style='margin-bottom:0in;margin-bottom:.0001pt;text-align:right;line-height:normal'><span style='font-family:\"Arial\",sans-serif;'>");

  template.print(vs.get(count).getLabel());

  template.print("</span></p></td><td style='border:none;border-bottom:solid #BFBFBF 1.0pt;background:#E7EBF9;padding:2.9pt 2.9pt 2.9pt 2.9pt'><p style='margin-bottom:0in;margin-bottom:.0001pt;line-height:normal'><b>");

  if(vs.get(count).getDisplayValue().toString().indexOf('\n') != -1){

  var text = vs.get(count).getDisplayValue().toString().split('\n');

  var textToPrint='';

  for(var k=0; k<text.length; k++){

  textToPrint = textToPrint + "<span style='font-family:\"Arial\",sans-serif;color:black;'>" + text[k] + "</span>\n";

  }

  template.print(textToPrint);

  }

  else{

  template.print("<span style='font-family:\"Arial\",sans-serif;color:black;'>");

  template.print(vs.get(count).getDisplayValue());

  template.print("</span></b></p></td></tr>");

  }

 

      }

      count = Math.floor(count + 1);

}

Could anyone hep me to solve this issue.

12 REPLIES 12

Hi Jon,



I use a similar script to print non-blank variables on sc_req_item notifications. I can't seem to get it to work for sc_task's where it just pulls in the variables from only that task though. You mention using the above script for sc_task notifications. How do you make the script only return variables on a specific catalog task?


Hi Chase,



All my notifications are event-based.   When a new sc_task is created, or depending on what's been updated on it, an event fires and kicks off the appropriate notification.   I then have four separate email scripts that are configured in a template.   All sc_task notifications, (req item, incident, approval, etc...) have their own template.



In the script that pulls the variables from the appropriate sc_task, I've the following.   Keep in mind, I've only been at this less than a year and have yet to attend any JS training so there are probably better methods to use for some of this but the following is working so it's what I'm going with now.



var ReqFor = current.request_item.u_requested_for;
var VIP = ReqFor.vip;
var Loc = ReqFor.location;
var Location = Loc.getDisplayValue();
var DeskPhone = ReqFor.phone;
var MobilePhone = ReqFor.mobile_phone;


var grLoc = new GlideRecord('cmn_location');
grLoc.get(Loc);
var Country = grLoc.country;



...



var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(current.request_item);
set.load();
var q = set.getFlatQuestions();
var iter = q.iterator();
while (iter.hasNext()) {
var item = iter.next();

if (item.getLabel() != 'null' && item.getLabel() != 'Urgency' && item.getLabel() != 'Impact' && item.getDisplayValue() != '' && item.getDisplayValue() != 'None' && item.getDisplayValue() != 'null' && item.getDisplayValue() != 'false') {
 
  if (item.name == 'request_for' || item.name == 'request_by') {
   
    template.print("<span><p><p style='font-family: calibri; font-size: 14.5px'><b>" + item.getLabel() + "</b> - " + item.getDisplayValue());
    template.print("<br><b>Location</b> - " + Location);
    template.print("<br><b>Country</b> - " + Country);
   
    if (!DeskPhone.nil())
      template.print("<br><b>Business Phone</b> - " + DeskPhone);
   
    if (!MobilePhone.nil())
      template.print("<br><b>Mobile Phone</b> - " + MobilePhone + "</p></span>");
  }
  else
    template.print("<span><p><p style='font-family: calibri; font-size: 14.5px'><b>" + item.getLabel() + "</b> - " + item.getDisplayValue() + "</p></span>");
}
}


template.print("<p><p style='font-family: calibri; font-size: 14.5px'><b>Urgency</b> - " + current.urgency.getDisplayValue() + "</p>");


template.print("<p><p style='font-family: calibri; font-size: 14.5px'><b>Priority</b> - " + current.priority.getDisplayValue() + "</p>");


Thank you much Jon. This has helped set me on the right track.


Robert Chrystie
ServiceNow Employee
ServiceNow Employee

Hi Ajai,



I would recommend using the 'Visible on Summaries' checkbox on variables and adding a check in this script to see if they should be visible or not.



This can easily be done by adding 'vs.get(i).isVisibleSummary()' as a condition in the if statement.   Here is how I was able to implement this.



// Get the variable pool for this item


var variablePool = new GlideappVariablePoolQuestionSet();


variablePool.setRequestID(current.sysapproval);


variablePool.load();


var questions = variablePool.getFlatQuestions();



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



        // Check if this question should be displayed


        var item = questions.get(i);


        if(item.getLabel() != '' && item.isVisibleSummary()) {



                  // Display the question and answer


                  template.space(4);


                  template.print('         <b>' +   item.getLabel() + "</b>: " + item.getDisplayValue() + "<br />");



        }


}


Hey Robert



This looks like exactly what I'm after and I think I'm very close to getting this solution running - I had to modify some stuff that had already been done by another developer, but I believe it should be more or less what you have here you can see me attempting to just print the method out:



var set = new GlideappVariablePoolQuestionSet();


          set.setRequestID(item.sys_id);


          set.load();


          var vs = set.getFlatQuestions();


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



          var varItem = vs.get(i);



          if(varItem.getLabel() != ''/* && varItem.isVisibleSummary()*/) {


          template.print("<tr><td>" +   varItem.getLabel() +varItem.isVisibleSummary() +   "</td><td>" + varItem.getDisplayValue() + "</td></tr>");  


              }


          }



however whenever I try to call the <variable>.isVisibleSummary() as above it drops an error in the log:



java.lang.SecurityException: Illegal access to method isVisibleSummary() in class com.glideapp.questionset.Question


    Caused by error in Email Script: 'clean.request.itil.approve.role_script_1' at line 38


        35:               var varItem = vs.get(i);


        36:


        37:               if(varItem.getLabel() != ''/* && varItem.isVisibleSummary()*/) {


==>   38:               template.print("<tr><td>" +   varItem.getLabel() +varItem.isVisibleSummary() +   "</td><td>" + varItem.getDisplayValue() + "</td></tr>");  


        39:                 }


        40:             }


        41:             template.print("       </table>\n");



I'm stumped here - the method looks fine as far as I can tell, but I'm not allowed to call isVisibleSummary from a mail script (which is what you did I think?).   I'm going to compare to a virgin instance then start digging around in the script include method (assuming I have access to it).