Format table in Notification Email Script – ServiceNow

Hello ServiceNow Geeks,

Today we will cover another interesting feature we can easily develop in ServiceNow, i.e., Notification Email Scripts. In that we will look into a code to format table using notification email script in ServiceNow.

In the below code, we are formatting a table for details captured in import set row table. Columns are target record, state, transform map name and comment for import so done.

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

function rowsImported() {
	template.print("<p style='font-size:20px'>" + 'Below the status of data inserted: </b>' + "</p>");
	template.print( "<table border='1' width='700'>" ); 
	template.print( "<tr>" ); 
	template.print( "<th><center>" + 'Target Record' + "</center></th>" );
	template.print( "<th><center>" + 'State' + "</center></th>" );
	template.print( "<th><center>" + 'Transform Map' + "</center></th>" ); 
	template.print( "<th><center>" + 'Comment' + "</center></th>" ); 
	template.print( "</tr>" ); 
	var gr = new GlideRecord('sys_import_set_row');
	gr.addQuery('sys_import_set',current.sys_id);
	gr.query();
	while(gr.next()){
		template.print( "<tr>" ); 
		template.print("<td><center>" +gr.sys_target_sys_id.getDisplayValue() + "</center></td>");
		template.print("<td><center>" + gr.sys_import_state.getDisplayValue() + "</center></td>");
		template.print("<td><center>" +gr.sys_transform_map.getDisplayValue() + "</center></td>");
		template.print("<td><center>" +gr.sys_import_state_comment.getDisplayValue() + "</center></td>");
		template.print( "</tr>" );
	}
	template.print( "</table>" );
}

})(current, template, email, email_action, event);

template.print() function provides this feature to include HTML code in notification email scripts in ServiceNow.

Also, GlideRecord is used to fetch all these details from import set row table which matches our import sets.

Leave a Reply

Your email address will not be published. Required fields are marked *