Mail script alignment

Prem13
Tera Contributor

im trying to display data in table format on notification using email script but the alignment is not being properly set.

below is the script

(function runMailScript(current, template, email, email_action, event) {

    var inc = new GlideRecord("incident");
    inc.addEncodedQuery("state!=7");
    inc.query();
    template.print("<table style=\"text-align:left;background-color:F2F3F;border-collapse:collapse;font-family:arial,helvetica;font-size:12pt;padding:5px; border:1px solid;border-color:grey;\">");
    template.print("<tr><td style=\"padding:5px;border:1px solid;border-color:grey;\">S.No</td>");
    template.print("<td style=\"padding:5px;border:1px solid;border-color:grey;\">Caller</td>");
    template.print("<td style=\"padding:5px;border:1px solid;border-color:grey;\"><strong>Short description<strong></td>");
    template.print("<td style=\"padding:5px;border:1px solid;border-color:grey;\"><strong>State<strong></td></tr>");
    while (inc.next()) {
      var i=0

            template.print("<tr><td style=\"padding:5px;border:1px solid;border-color:grey;\">");
			template.print(i);
            template.print("</td><td style=\"padding:5px;border:1px solid;border-color:grey;\">" + inc.caller_id.getDisplayValue() + '</td>');
            template.print("<td style=\"padding:5px;border:1px solid;border-color:grey;\">" + inc.short_description + '</td>');
            template.print("<td style=\"padding:5px;border:1px solid;border-color:grey;\">" + inc.state.getDisplayValue() + '</td></tr>');
            
    
		
    }
template.print('</table>');
})(current, template, email, email_action, event);

the table is not aligned properly , and the table only applies for first record and its not applied for the second record

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

Can you try this:

(function runMailScript(current, template, email, email_action, event) {
    var inc = new GlideRecord("incident");
    var htmlTable = '';
    inc.addEncodedQuery("state=7");
    inc.query();
    var i = 1;
    htmlTable += "<table style='text-align:left;background-color:F2F3F;border-collapse:collapse;font-family:arial,helvetica;font-size:12pt;padding:5px; border:1px solid;border-color:grey;'>";
    htmlTable += "<tr><th style='padding:5px;border:1px solid;border-color:grey;'>S.No</th>";
    htmlTable += "<th style='padding:5px;border:1px solid;border-color:grey;'>Caller</th>";
    htmlTable += "<th style='padding:5px;border:1px solid;border-color:grey;'><strong>Short description<strong></th>";
    htmlTable += "<th style='padding:5px;border:1px solid;border-color:grey;'><strong>State<strong></th></tr>";
    while (inc.next()) {
        htmlTable += "<tr><td style='padding:5px;border:1px solid;border-color:grey;'>";
        htmlTable += i;
        htmlTable += "</td><td style='padding:5px;border:1px solid;border-color:grey;'>" + inc.caller_id.getDisplayValue() + '</td>';
        htmlTable += "<td style='padding:5px;border:1px solid;border-color:grey;'>" + inc.short_description + '</td>';
        htmlTable += "<td style='padding:5px;border:1px solid;border-color:grey;'>" + inc.state.getDisplayValue() + '</td></tr>';

        i++;

    }
    htmlTable += '</table>';
    template.print(htmlTable);
})(current, template, email, email_action, event);

 

Result:

find_real_file.png

View solution in original post

5 REPLIES 5

Maik Skoddow
Tera Patron
Tera Patron

Hi Prem,

I'm afraid it is not clear, what your question is.

The only thing I can say is, that

  • variable i is not incremented and therefore always remains 0,
  • table alignment is better solved with HTML attributes: <table align="center">
  • text formatting should be better done within a sourrounding <div> or in each cell but not at <table>
  • there are to many unnecessary escapings, which increase risk of erros. Better write 
    template.print('<td style="padding:5px;border:1px solid;border-color:grey;"><strong>State<strong></td></tr>');

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Hi Maik,

Actually the values are not properly aligned , i dont see any issues with the script.

i made some minor  tweaks as you suggested still the same.

(function runMailScript(current, template, email, email_action, event) {
var inc = new GlideRecord("incident");
    inc.addEncodedQuery("state=7");
    inc.query();
	var i=1;
    template.print("<table style='text-align:left;background-color:F2F3F;border-collapse:collapse;font-family:arial,helvetica;font-size:12pt;padding:5px; border:1px solid;border-color:grey;'>");
    template.print("<tr><td style='padding:5px;border:1px solid;border-color:grey;'>S.No</td>");
    template.print("<td style='padding:5px;border:1px solid;border-color:grey;'>Caller</td>");
    template.print("<td style='padding:5px;border:1px solid;border-color:grey;'><strong>Short description<strong></td>");
    template.print("<td style='padding:5px;border:1px solid;border-color:grey;'><strong>State<strong></td></tr>");
    while (inc.next()) {
    

            template.print("<tr><td style='padding:5px;border:1px solid;border-color:grey;'>");
			template.print(i);
            template.print("</td><td style='padding:5px;border:1px solid;border-color:grey;'>" + inc.caller_id.getDisplayValue() + '</td>');
            template.print("<td style='padding:5px;border:1px solid;border-color:grey;'>" + inc.short_description + '</td>');
            template.print("<td style='padding:5px;border:1px solid;border-color:grey;'>" + inc.state.getDisplayValue() + '</td></tr>');
            
    i=i+1;
		
    }
template.print('</table>');
	})(current, template, email, email_action, event);

output:

find_real_file.png

Hi,

I think I found the problem. In your table header row two times a unclosed <strong> tag is defined.

Please make sure that all opened tags are closed later.

<strong>Short description</strong>

 

Kind regards

Maik

Willem
Giga Sage
Giga Sage

Can you try this:

(function runMailScript(current, template, email, email_action, event) {
    var inc = new GlideRecord("incident");
    var htmlTable = '';
    inc.addEncodedQuery("state=7");
    inc.query();
    var i = 1;
    htmlTable += "<table style='text-align:left;background-color:F2F3F;border-collapse:collapse;font-family:arial,helvetica;font-size:12pt;padding:5px; border:1px solid;border-color:grey;'>";
    htmlTable += "<tr><th style='padding:5px;border:1px solid;border-color:grey;'>S.No</th>";
    htmlTable += "<th style='padding:5px;border:1px solid;border-color:grey;'>Caller</th>";
    htmlTable += "<th style='padding:5px;border:1px solid;border-color:grey;'><strong>Short description<strong></th>";
    htmlTable += "<th style='padding:5px;border:1px solid;border-color:grey;'><strong>State<strong></th></tr>";
    while (inc.next()) {
        htmlTable += "<tr><td style='padding:5px;border:1px solid;border-color:grey;'>";
        htmlTable += i;
        htmlTable += "</td><td style='padding:5px;border:1px solid;border-color:grey;'>" + inc.caller_id.getDisplayValue() + '</td>';
        htmlTable += "<td style='padding:5px;border:1px solid;border-color:grey;'>" + inc.short_description + '</td>';
        htmlTable += "<td style='padding:5px;border:1px solid;border-color:grey;'>" + inc.state.getDisplayValue() + '</td></tr>';

        i++;

    }
    htmlTable += '</table>';
    template.print(htmlTable);
})(current, template, email, email_action, event);

 

Result:

find_real_file.png