Useful code to generate a HTML table from a GlideRecord Query. This is especially useful for when you need to embed content in an email notification.
Script Include Example:
var grHTMLTable = Class.create();
grHTMLTable.prototype = {
initialize: function() {},
grHTMLtable: function(grTable, grQuery, grFieldLabels, grColumns) {
/*
grTable = tableName [string]
grQuery = encodedQuery [string]
grFieldLabels = Field Labels [array]
grColumns = column Names [array]
*/
var output = '';
var table = grTable;
var query = grQuery;
var fieldLabels = grFieldLabels;
var fields = grColumns;
var grHTML = new GlideRecordSecure(table);
grHTML.addEncodedQuery(query);
grHTML.query();
if (grHTML.hasNext()) {
output = '<table style = "font-family: arial, sans-serif; border-collapse: collapse; width: 100%;"> <tr> '
for (var i = 0; i < fieldLabels.length; i++) {
output = output + '<th style = "border: 1px solid #dddddd; text-align: left; padding: 8px;">' + fieldLabels[i] + '</th>'
}
while (grHTML.next()) {
output = output + '<tr>';
for (var j = 0; j < fields.length; j++) {
output = output + '<td style = "border: 1px solid #dddddd; text-align: left; padding: 8px;">' + grHTML.getDisplayValue(fields[j]) + '</td>';
}
output = output + '</tr>';
}
output = output + '</table>';
}
return output;
},
type: 'grHTMLTable'
};
Mail Script Example:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var table = 'incident';
var query = 'stateIN1,2,3^assignment_group!=NULL';
var fieldLabels = ['Number', 'Short Description', 'State','Assignment Group', 'Assigned To'];
var fields = ['number', 'short_description','state','assignment_group', 'assigned_to'];
var results = new grHTMLTable().grHTMLtable(table, query, fieldLabels, fields);
template.print(results);
})(current, template, email, email_action, event);
Call Mail Script in Notification:
${mail_script:showIncidentsHTMLTable}
Results


One response to “GlideRecord HTML Table – Reusable Code”
[…] Results (combined with GlideRecord HTML Table – Reusable Code) […]
LikeLike