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

SteveS
Kilo Expert

I wanted to add the same option to my catalog emails so that variables will display. Are you putting this right into a notification and are you putting it at the Request, req_item or sc_task level when your sending it?


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>


SharpAl
Kilo Contributor

Thanks for this script. This is the first thread mentioning sorting that has solved it. Nice work!


SharpAl
Kilo Contributor

Here's a version of the UI Macro "approval_summarizer_sc_req_item" based on the code from this post:

NB: It also hides variables with a value of false.



<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<tr>
<td class="label_left" width="100%">
<label style="margin-left: 10px"><b> ${gs.getMessage('Summary of Requested Item being approved')}: </b>
<g:label_spacing/> </label>
</td>
</tr>
<g:evaluate var="jvar_ni" expression="

var sc_req_item = ${ref}.sysapproval;
"/>

<tr>
<td width="100%">
<table width="100%">

<tr>
<td class="label_left" width="300px">
<label style="margin-left: 10px">${sc_req_item.cat_item.sys_meta.label}: </label>
</td>
<td>
${sc_req_item.cat_item.name}
</td>
</tr>
<tr>
<td class="label_left" width="300px">
<label style="margin-left: 10px">${gs.getMessage('Request Number')}:</label>
</td>
<td> ${sc_req_item.request.number.getDisplayValue()}
<input style="visibility: hidden" NAME="make_spacing_ok"></input> </td>
</tr>

<tr>
<td class="label_left" width="300px">
<label style="margin-left: 10px">${gs.getMessage('Requested for user')}:</label>
</td>
<td>
${sc_req_item.request.requested_for.getDisplayValue()}
</td>
</tr>

<tr>
<td class="label_left" width="300px">
<label style="margin-left: 10px">${gs.getMessage('Request created by')}:</label>
</td>
<td>
${sc_req_item.request.opened_by.getDisplayValue()}
</td>
</tr>
<g:evaluate var="ni" expression="
var totalPrice = sc_req_item.price.toString();
if (totalPrice != '') {
totalPrice = parseFloat(totalPrice);
totalPrice = totalPrice.toFixed(2);
var totalPriceIsZero = false;
}
"/>
<tr>
<td class="label_left" width="300px">
<label style="margin-left: 10px">${sc_req_item.price.sys_meta.label}: </label>
</td>
<td>
${totalPrice}
</td>
</tr>
<tr>
<td class="label_left" width="300px">
<label style="margin-left: 10px">${sc_req_item.u_price_detail.sys_meta.label}: </label>
</td>
<td>
${sc_req_item.u_price_detail}
</td>
</tr>

<tr>
<td class="label_left" width="300px">
<label style="margin-left: 10px">${sc_req_item.request.special_instructions.sys_meta.label}: </label>
</td>
<td>
${sc_req_item.request.special_instructions}
</td>
</tr>
<tr>
<td colspan="2">
$[SP]
</td>
</tr>
<tr>
<td class="label_left" colspan="2">
<b> Requested Item Variables: </b>
</td>
</tr>
</table>
</td>
</tr>
<j:set var="jvar_line_num" value="0" />
<tr>
<td width="100%">
<table width="100%">

<j:set var="jvar_line_color" value="odd"/>
<j:set var="jvar_line_num" value="${jvar_line_num + 1}"/>
<j:if test="${jvar_line_num % 2 == 0}">
<j:set var="jvar_line_color" value="even"/>
</j:if>
<g:evaluate var="ni" expression="
var smart_description = sc_req_item.cat_item.short_description;
if (smart_description == null || smart_description == '' || smart_description == 'undefined')
smart_description = sc_req_item.cat_item.name;
"/>

<g:evaluate jelly="true">
var varown = new GlideRecord('sc_item_option_mtom');
varown.addQuery("request_item", current.sysapproval);
varown.query();
</g:evaluate>


<j:while test="${varown.next()}">
<j:set var="jvar_visible" value="${varown.sc_item_option.item_option_new.visible_summary}"/>
<g:evaluate var="jvar_label">
var question = Packages.com.glideapp.questionset.Question.getQuestion(varown.sc_item_option.item_option_new);
question.setValue(varown.sc_item_option.value);
question.getLabel();
</g:evaluate>
<g:evaluate var="jvar_display_value">
var question = Packages.com.glideapp.questionset.Question.getQuestion(varown.sc_item_option.item_option_new);
question.setValue(varown.sc_item_option.value);
question.getDisplayValue();
</g:evaluate>
<j:if test="${jvar_label != ''}" >
<j:if test="${jvar_display_value != ''}" >
<j:if test="${jvar_display_value != 'false'}" >
<j:if test="${jvar_visible == 'true'}" >
<tr>
<td class="label_left" width="300px">
<label style="margin-left: 10px">${jvar_label}: </label>
</td>
<td>${jvar_display_value}
<input style="visibility: hidden" NAME="make_spacing_ok"></input>
</td>
</tr>
</j:if>
</j:if>
</j:if>
</j:if>
</j:while>


</table>
</td>
</tr>
</j:jelly>