Format variables in mail script to display in HTML table

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2019 03:47 PM
I have some email notifications that are using a mail script to get variable information and display it in the body of the email using the code below:
var item = new GlideRecord("sc_req_item");
item.addQuery('sys_id', current.request_item);
item.query();
while(item.next()) {
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() != '' && vs.get(i).getDisplayValue() != '') {
template.space(4);
template.print('<strong>'+ vs.get(i).getLabel() + ' : </strong>');
template.print(vs.get(i).getDisplayValue() + "<br />");
}
}
}
This works fine, but the formatting isn't consistent with other notifications that we use, and I'd like to effectively do this same thing, but apply some HTML formatting found in our other email templates shown below:
<table border="0" width="90%" align="center">
<tbody>
<tr bgcolor="#f0f0f0">
<td valign="top" width="33%">
<p><strong>Requested by:</strong></p>
</td>
<td valign="top" width="77%">
<p>${requested_for}</p>
</td>
</tr>
<tr>
<td valign="top" width="33%">
<p><strong>Title:</strong></p>
</td>
<td valign="top" width="77%">
<p>${short_description}</p>
</td>
</tr>
<tr bgcolor="#f0f0f0">
<td valign="top" width="33%">
<p><strong>Description:</strong></p>
</td>
<td valign="top" width="77%">
<p>${description}</p>
</td>
</tr>
</tbody>
</table>
What I'm not sure of is whether the variables that are getting pulled by the mail script can be formatted in the same way where the Label and Display value are in different columns of the row, and each new variable creates a new row with the proper formatting.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2019 03:51 PM
Hi Marcel,
You will need to template.print() all of the HTML code that makes up the table as well as it's content.
My advice would be to use multiple template.print() statements so that the code is still somewhat human readable.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2019 06:22 PM
Hi Marcel,
As Luke replied, you will have to use template.print() to print the HTML formatting in your email notification. But if you want to reuse that template, for maybe other notifications, I would advise to create a Script Include function which returns the formatted HTML for the table, which takes the headers and values as parameters. Then you can use that function in multiple email scripts to get the formatting done.
Thanks,
Manish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2019 05:43 PM
Thanks for the feedback. I haven't had a chance to swing back to this quite yet, but have done something similar with template.print in a few other places to make a formatted table that looks like the others we've had on other notifications.
I think that the challenge that I'm having here is that I'm not quite sure how to apply styles to alternating rows when it's printing the Label and Display Value of the individual variables since the mail script is pulling anything that isn't blank I believe, and the number or rows would vary.
As a reference, this is what the HTML table looks like in other notifications that we have currently: