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

Awesome, glad someone else found this to be useful!!


I saw that you were able to sort the names out with the script you came up with. What can I put in my script to sort insteading of using your script?Here is the script I'm using.

Your approval/rejection is required on the following IT Service Desk item. Please review this item and use the links below to take the necessary action.

Item Description: ${sysapproval.short_description}
Requested For: ${sysapproval.request.requested_for}


var unitPrice = current.sysapproval.price.toString();
var qty = current.sysapproval.quantity.toString();

if (unitPrice != '') {
unitPrice = parseFloat(unitPrice);
unitPrice = unitPrice.toFixed(2);
}s

var totalPrice = unitPrice * qty;

if (totalPrice != '') {
totalPrice = parseFloat(totalPrice);
totalPrice = totalPrice.toFixed(2);
}

template.print('Unit price: $' + unitPrice + '\n');
template.print('Quantity: ' + qty + '\n');
template.print('Total: $' + totalPrice + '\n');

template.print('\nRequest Item Details:\n');
//
// Run through the variables
//
var key;
template.print('\n');

for (key in current.sysapproval.variables) {
var currentVar = current.sysapproval.variables[key];

if (currentVar.getDisplayValue() != 'false' && currentVar.getDisplayValue() != '') {
template.print('\n');
}
}
template.print('


if (currentVar.getDisplayValue() == 'true')
template.print(' colspan="2"');

template.print('>');
template.print(currentVar.getGlideObject().getQuestion().getLabel());

if (currentVar.getDisplayValue() != 'true') {
template.print('
'+ currentVar.getDisplayValue());
}
template.print('
');


Special Instructions:
${sysapproval.request.special_instructions}



${mailto:mailto.approval} via email


${mailto:mailto.rejection} via email


Click here to view Approval Request: ${URI}
Click here to view the details of the ${sysapproval.sys_class_name}: ${sysapproval.URI}


rsanon
Tera Contributor

@ wattsj

What if you want a print-out of the variables after the Requested Item has been provision to completion, not at insert? What condition could we use?


Hi Joe - I tried putting this script into an email script, but we received an error saying:



Illegal attempt to access class 'com.glideapp.questionset' via script



Do you know what we might need to adjust? Thanks!


Are you using this script calls anywhere in your script - "Packages.com.glideapp.questionset" ? You can chk this below link to replace the packages call



Packages Call Replacement Script Objects - ServiceNow Wiki