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

Thank you, Muhammad! I appreciate your help! 🙂 

Hi @Muhammad Khan, this script has been working thank you so much,

Would you be able to help me again in the Column alignment, i have a requirement to update this 

 

Left Align: Resource Type, Vendor, Short Description

Right Align: Total and the grand total the "13099.33" text below.

 

Thank you!

 

Current script:

 

var approvalFor = current.sysapproval.getRefRecord();
var proj;
var query;
var dmnd = new GlideRecord('dmn_demand');
dmnd.addQuery("sys_id", current.sysapproval);
dmnd.query();
if (dmnd.next()) {
proj = dmnd.project;
}

template.print('<style>th table,th,td{text-align: left; border-color: #404040; border-collapse: collapse;} </style><table style="width:90%";><tr><th>Resource Type</th><th>Vendor</th><th>Short Description</th><th>Total</th></tr>');

var total = 0;

var cost_plan = new GlideRecord('cost_plan');
cost_plan.addQuery("task", proj);
cost_plan.query();
if (cost_plan.hasNext()) {

while (cost_plan.next()) {
template.print('<tr><td>' + cost_plan.resource_type.getDisplayValue() + '</td><td>' + cost_plan.u_vendor.getDisplayValue() + '<td>' + cost_plan.name + '<td>' + cost_plan.cost_local_currency + '</td></tr>');
total += parseFloat(cost_plan.cost_local_currency);

}
template.print('<tr><td style="border: none"/><td style="border: none"/><td style="border: none"/><td>' + total + '</td></tr>');
template.print('</table>');

}

 

 

 

Screen Shot 2022-12-15 at 8.05.44 AM.png