Showing Request Item / Catalog Variables in Mail Script

wattsj
Kilo Expert

I'm using an email notification to send out information regarding approvals on RITM's. The information I want to send will contain the variable information from the item that was ordered.

I stole this script from another email notification:


template.print("Summary of Requested item:\n");
var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", current.sysapproval);
gr.query();
while(gr.next()) {
template.print(gr.number + ": " + gr.quantity + " X " + gr.cat_item.getDisplayValue() + "\n");
template.print(" Options:\n");
for (key in gr.variables) {
var v = gr.variables[key];
if(v.getGlideObject().getQuestion().getLabel() != '' && v.getDisplayValue() != '') {
template.space(4);
template.print(' ' + v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");
}
}
}


My only issue is that the variables don't display in the proper order. You would think this would be an easy thing to accomplish, but I've been stuck for awhile. Any ideas?

1 ACCEPTED SOLUTION

Yes, you can put a mail script right into the message. I have this setup on an approval request, here's the details:

table: sysapproval_approver
event: approval.inserted

and then I have a condition on the notification so that it only sends out for RITMs:

Approval for.Number "starts with" RITM

And finally here is my mail script inside the message of the email (this looks a little different than the one I posted, it helped me get the variables in order):



<mail_script>
template.print("Summary of Requested item:\n");
var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", current.sysapproval);
gr.query();
while(gr.next()) {
template.print(gr.number + ": " + gr.quantity + " X " + gr.cat_item.getDisplayValue() + "\n");
template.print(" Options:\n");

var varown = new GlideRecord('sc_item_option_mtom');
varown.addQuery("request_item", current.sysapproval);
varown.query();
while (varown.next()){
var visible = varown.sc_item_option.item_option_new.visible_summary;
var question = Packages.com.glideapp.questionset.Question.getQuestion(varown.sc_item_option.item_option_new);
question.setValue(varown.sc_item_option.value);
if (question.getLabel() != "" &amp;&amp; question.getDisplayValue() != "" &amp;&amp; visible == true){
template.space(4);
template.print(' ' + question.getLabel() + " = " + question.getDisplayValue() + "\n");
}
}

}
</mail_script>


View solution in original post

36 REPLIES 36

Hi Kristen,



Thanks for your reply. The Notification is on "sysapproval_approver" table



Steps followed,



1. Created and Email script



2. Called that script in a   Email Template



3. Added this template to a Notification.




I Just tried to Print the out of current.sysapproval   and i am getting the sys_id of the approver.



Script:



(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,


                  /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,


                  /* Optional GlideRecord */ event) {




template.print("Summary of Requested items:<br />");  


var item = new GlideRecord("sc_req_item");


item.addQuery("sys_id", current.sysapproval);


item.query();


while(item.next()) {


        template.print(item.number + ":   " + item.quantity + " X " + item.cat_item.getDisplayValue() + " at " + item.cat_item.price.getDisplayValue() + " each <br />");


        template.print("       Options:<br />");


        var keys = [];


        var set = new GlideappVariablePoolQuestionSet();


        set.setRequestID(item.sys_id);


        set.load();


        var vs = set.getFlatQuestions();


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


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


                  template.space(4);


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


            }


        }


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


Is the approval on the request or the requested item? If request, then change item.addQuery("sys_id",current.sysapproval) to item.addQuery("request",current.sysapproval). I have a check on the current.sysapproval.sys_class_name to run my query on the RITMs correctly from the approval.


How could I only show variables that have a value?


This is the condition I have scripted for including the variables:



if (vs.get(i).getLabel() != '' && vs.get(i).getDisplayValue()!='' && vs.get(i).getDisplayValue()!='false' && vs.get(i).getLabel() != 'Workflow' && vs.get(i).getLabel() != 'Requested for' && vs.get(i).getLabel() != 'Phone number' && vs.get(i).getLabel() != 'Order type') {}



the getDisplayValue()!='' filters out any variables that have a blank value. I also only include checkboxes when "true", so .getDisplayValue()!='false' filters those.


One more question: Any idea how to expand/collapse that section in an email?