Email Scripting Help - Catalog Task Variables

chaselong
Mega Expert

I have an e-mail script (which I originally got from a post here and modified - thank you SN community!) that I use to print the true / not empty variables of request items in various e-mails. It works beautifully, but I'd like to modify it to be used on catalog task emails. The ideal scenario is that it prints out only the true / not empty request items variables that have been pushed down to the specific catalog task, so that when the assignment group working the task gets a notification they don't see all of the request item variables, just the ones relevant to them. I've seen some scripts here that print catalog task variables, but none are quite what I'm looking for. Any help would be appreciated.

(function runMailScript(current, template, email, email_action, event) {

  template.print('<font size="3" color="#808080" face="helvetica">');

  template.print('<strong>Request Item Variables:</strong><br>');

  var set = new GlideappVariablePoolQuestionSet();

  set.setRequestID(current.sys_id.toString());

  set.load();

  var vs = set.getFlatQuestions();

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

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

              template.space(4);

              template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br />");

  }

  }

  template.print('</font>');

})(current, template, email, email_action, event);

1 ACCEPTED SOLUTION

chaselong
Mega Expert

Thanks so much for your help Raymond. I actually ended up finding a bit of code from another post that led me to edit my original script to limit it to printing the variables of the specific catalog task. Including the revised script below for anyone else who may find this helpful.



(function runMailScript(current, template, email, email_action, event) {


  template.print('<font size="3" color="#808080" face="helvetica">');


  template.print('<strong>Request Item Variables:</strong><br>');


  var set = new GlideappVariablePoolQuestionSet();


  set.setRequestID(current.request_item.sys_id.toString());


  set.setTaskID(current.sys_id.toString());


  set.load();


  var vs = set.getFlatQuestions();


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


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


              template.space(4);


              template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br />");


  }


  }


  template.print('</font>');


})(current, template, email, email_action, event);


View solution in original post

4 REPLIES 4

randrews
Tera Guru

what i did was to create a notification email script that i can throw into any catalog item/task to put the variables into it..... i suspect if you drop this in and call it in your notification it will print the variables for you unless you have done some customization.. and it will work on tasks, items, and requests.





// Start the table


template.print("<div style='margin-top:30px;margin-bottom:10px;'><strong>DETAILS:</strong><hr></div>");



var u_sid='';


var table_name = current.getTableName();


var totalPrice = 0;



switch(table_name.toString()){


      case('sc_req_item'):


      totalPrice = current.request.price;


      u_sid = current.sys_id;


      get_vars();


      break;



      case('sc_task'):


      u_sid = current.request_item.sys_id;


      get_vars();


      break;



      case('sc_request'):


      totalPrice = current.price;


      var gr = new GlideRecord("sc_req_item");


      gr.addQuery("request", current.sys_id);


      gr.query();


      while(gr.next()) {                      


              u_sid = gr.sys_id;


              get_vars();


      }


      break;



      case('sysapproval_approver'):


      totalPrice = current.sysapproval.price;


      var approval_tablename = current.sysapproval.sys_class_name;


      switch(approval_tablename.toString()){


              case('sc_req_item'):


              u_sid = current.sysapproval;


              get_vars();


              break;



              case('sc_request'):


              var gr = new GlideRecord("sc_req_item");


              gr.addQuery("request", current.sysapproval.sys_id);


              gr.query();


              while(gr.next()) {


                      u_sid = gr.sys_id;


                      get_vars();


              }


              break; // End sc_request case      


              default: // If no case found do nothing


              break; // End default


              }


             


      break; // End sysapproval_approver case


      default: // If no case found, do nothing


      break; // End default


}



function get_vars(){


      var item = new GlideRecord("sc_req_item");


      item.addQuery("sys_id", u_sid);


      item.query();


      while(item.next()){


              var nicePrice = item.price.toString();


              if (nicePrice != ''){


                      nicePrice = parseFloat(nicePrice);


                      nicePrice = nicePrice.toFixed(2);


              }


              var howMany = item.quantity;


              var keys = [];


              var set = new Packages.com.glideapp.servicecatalog.variables.VariablePoolQuestionSet();      


              set.setRequestID(item.sys_id);


              set.load();


              var vs = set.getFlatQuestions();



              // If no price, print item name


              template.print("<table cellspacing='0' cellpadding='5' border='0' style='font-size:14px;'>");


              if (nicePrice == "0.00"){


                      template.print("<tr><td colspan='2'><strong>" + item.cat_item.name + "</span></strong></td></tr>");


              }


              // Otherwise, print item nam, price and quantity


              else{


                      template.print("<tr><td colspan='2'><strong>" + item.cat_item.name + " - <span style='color:#169e58'>$" + nicePrice + " x " + item.quantity + "</span></strong></td></tr>");


              }



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


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


                            && (vs.get(i).getLabel() != "Requested for")


                            && (vs.get(i).getLabel() != "Request manager approval")


                            && (vs.get(i).getDisplayValue() != "-- None --")


                            && (vs.get(i).getDisplayValue() != "")


                            && (vs.get(i).getDisplayValue() != "false")){


                     


                      template.print("<tr><td valign='top' align='left' style='padding-left:10px;width:5px;'>&middot;</td><td align='left'><span style='color:#555555'>" + vs.get(i).getLabel() + ":    </span>" + vs.get(i).getDisplayValue() + "</td></tr>");


              }


      }


     


      // End the table and add a break


      template.print("</table>");


      template.print("<br>");


      }


}



// Calculate the total price


var niceTotalPrice =   totalPrice.toString();


if (niceTotalPrice != ''){


      niceTotalPrice = parseFloat(niceTotalPrice);


      niceTotalPrice = niceTotalPrice.toFixed(2);


     


      // If Total price is not 0.00, add the total price


      if (niceTotalPrice != "0.00"){


              template.print("<table cellspacing='0' cellpadding='5' border='0' style='font-size:14px !important;'>");


              template.print("<tr><td><strong>Total - <span style='color:#169e58'>$" + niceTotalPrice + "</strong></td></tr></table><br>");


      }


}


I tried your script and called it in a notification on the sc_task table. Unfortunately, it didn't print any variables for me. It just printed "Details:" and a divider line.


ok that means it got as far as the first template print but it doesn't sound like it got a valid entry into the case structure...



put a template print of table name after the variable definitions so you can see what it enters the switch with.... if that works and you have a valid table name.. than in that case template print the sid to verify it is getting a valid sid before it calls get_vars.



i would also throw a template print in both the functions to see when it goes into them....


chaselong
Mega Expert

Thanks so much for your help Raymond. I actually ended up finding a bit of code from another post that led me to edit my original script to limit it to printing the variables of the specific catalog task. Including the revised script below for anyone else who may find this helpful.



(function runMailScript(current, template, email, email_action, event) {


  template.print('<font size="3" color="#808080" face="helvetica">');


  template.print('<strong>Request Item Variables:</strong><br>');


  var set = new GlideappVariablePoolQuestionSet();


  set.setRequestID(current.request_item.sys_id.toString());


  set.setTaskID(current.sys_id.toString());


  set.load();


  var vs = set.getFlatQuestions();


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


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


              template.space(4);


              template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br />");


  }


  }


  template.print('</font>');


})(current, template, email, email_action, event);