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

Site was down for maintenance, you should be able to open the URL now.


That was a quite a nice post which I have used for notifications.


Really found it useful to ignore blank variables, exclude specific variables via function.


I have done some customization to function which is used for excluding some of the variables though.


ramak
Giga Expert

Hi Joe, I tried implementing the code you shared using the QuestionSet package...it wouldn't work ! The OOTB code works but it shows all the variables even if its empty and in no particular order. (I added the 'OrderBy' query, still not working )



I am using a notification script based on a service catalog approval requested event. It shows 'Options' in the email notification and after that its empty. I am using GENEVA patch 6



Anything I am missing here ?


kiranhegde
Mega Expert

All,



I am trying to adapt the same script in my Helsinki version.



I am not not getting anything other than



"Summary of Requested item:"


We're using Helsinki as well and it seems to work for us. What table is your notification off of? I adjusted the above script for when I was sending notifications from the sc_req_item or sc_task (adjust the initial glide record query).