Created Table via Mail Script - help to center align, add border, grand total text in bottom right

Suz Roque
Tera Expert

Hi All!

We have a requirement to add a table in Email notification that will list all Order records(sample table). I have created a mail script that is called in the Notification body. Please see below Body for the sample created in Incident. This seems to be okay but it needs to be center aligned, with border and have to add a Grand total computation at the bottom right.  Please help me update this, below is the script used:

 

Screen Shot 2022-11-17 at 11.40.06 AM.png

 

 

It should be in this format + border

Screen Shot 2022-11-17 at 11.28.25 AM.png

Mail Script

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {

 

template.print('<table style="width:100%">');
template.print('<tr><th>Number</th><th>Short Description</th><th>State</th><th>Severity</th></tr>');
var gr = new GlideRecord("incident");
gr.addEncodedQuery("active=true^short_descriptionSTARTSWITHtester");
gr.query();
if (gr.getRowCount() > 0) {
while (gr.next()) {
template.print('<tr><td>' + gr.number + '</td><td>' + gr.short_description + '</td><td>' + gr.state + '</td><td>' + gr.severity + '</td></tr>');
}
template.print('</table>');
}
})(current, template, email, email_action, event);

1 ACCEPTED SOLUTION

This one looks better.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
	
    template.print('<style> th{background-color: #04AA6D;  color: white;} th,td{text-align: center; padding: 8px;} tr:nth-child(even){background-color: #f2f2f2;} tr:hover{background-color: #ddd;} </style><table style="font-family: Arial; border-collapse: collapse; width: 100%;"><tr><th>Number</th><th>Short Description</th><th>State</th><th>Price</th></tr>');

    var total = 0;
    var gr = new GlideRecord("sc_req_item");
    gr.addActiveQuery();
    gr.orderBy('number');
    gr.query();
    if (gr.hasNext()) {
        while (gr.next()) {
            template.print('<tr><td>' + gr.number + '</td><td>' + gr.short_description + '<td>' + gr.state.getDisplayValue() + '<td>' + '$' + gr.price + '</td></tr>');
            total += parseFloat(gr.price);
        }
        template.print('<tr><td/><td/><td><td>' + total + '</td></tr>');
        template.print('</table>');
    }
})(current, template, email, email_action, event);

 

Output

MuhammadKhan_1-1668666227415.png

 

 

View solution in original post

6 REPLIES 6

Muhammad Khan
Mega Sage
Mega Sage

Try this in email script.

template.print('<style>th {background-color: #404040;  color: white;} table,th,td{text-align: center; border: 1px solid black; border-color: #404040; border-collapse: collapse;} </style><table style="width:100%";><tr><th>Number</th><th>Short Description</th><th>Price</th></tr>');
	
	var total = 0;
	var gr = new GlideRecord("sc_req_item");
	gr.addActiveQuery();
    gr.orderBy('number');
    gr.query();
    if (gr.hasNext()) {
        while (gr.next()) {
            template.print('<tr><td>' + gr.number + '</td><td>' + gr.short_description + '<td>' + gr.price + '</td></tr>');
			total += parseFloat(gr.price);
        }
        template.print('<tr><td style="border: none"/><td style="border: none"/><td>' + total + '</td></tr>');
        template.print('</table>');
    }

 

 Output would look something like below.

MuhammadKhan_0-1668661923728.png

 

Thank you Muhammad, do you know how i can add another column after Short Description and before Price? also how can i spread it out to occupy the entire space, as the data looks compressed.

Try with below script.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
    template.print('<style>th {background-color: #404040;  color: white;} table,th,td{text-align: center; border: 1px solid black; border-color: #404040; border-collapse: collapse;} </style><table style="width:200%";><tr><th>Number</th><th>Short Description</th><th>State</th><th>Price</th></tr>');

    var total = 0;
    var gr = new GlideRecord("sc_req_item");
    gr.addActiveQuery();
    gr.orderBy('number');
    gr.query();
    if (gr.hasNext()) {
        while (gr.next()) {
            template.print('<tr><td>' + gr.number + '</td><td>' + gr.short_description + '<td>' + gr.state.getDisplayValue() + '<td>' + gr.price + '</td></tr>');
            total += parseFloat(gr.price);
        }
        template.print('<tr><td style="border: none"/><td style="border: none"/><td style="border: none"/><td>' + total + '</td></tr>');
        template.print('</table>');
    }
})(current, template, email, email_action, event);

This one looks better.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
	
    template.print('<style> th{background-color: #04AA6D;  color: white;} th,td{text-align: center; padding: 8px;} tr:nth-child(even){background-color: #f2f2f2;} tr:hover{background-color: #ddd;} </style><table style="font-family: Arial; border-collapse: collapse; width: 100%;"><tr><th>Number</th><th>Short Description</th><th>State</th><th>Price</th></tr>');

    var total = 0;
    var gr = new GlideRecord("sc_req_item");
    gr.addActiveQuery();
    gr.orderBy('number');
    gr.query();
    if (gr.hasNext()) {
        while (gr.next()) {
            template.print('<tr><td>' + gr.number + '</td><td>' + gr.short_description + '<td>' + gr.state.getDisplayValue() + '<td>' + '$' + gr.price + '</td></tr>');
            total += parseFloat(gr.price);
        }
        template.print('<tr><td/><td/><td><td>' + total + '</td></tr>');
        template.print('</table>');
    }
})(current, template, email, email_action, event);

 

Output

MuhammadKhan_1-1668666227415.png