Easy way to create on-the-fly Excel attachment on emails

While working on a project, I got asked by a customer – ‘Is it possible to add an on-the-fly attachment to a notification?’. That left me wondering as it was one of the most unusual requests. When we are sending out a notification from say an incident, there is no way to create & add an attachment to it (at least I haven’t noticed). So I set about creating one..

    1. The first step is to create data ready for csv file. In my scenario, I have to trigger an email notification with all stock items marked as ‘processed’. There will be a UI action on form which will raise an event for that email notification. I’m using Incident table for this.

var csv = '';
var movestockRec = new GlideRecord('u_movestock');
movestockRec.addQuery('u_processed', true);
movestockRec.addQuery('u_batch_no', source.u_batch_no);
while (movestockRec.next()) {
csv += movestockRec.u_part_number + ','
+ movestockRec.u_good_qty + ','
+ movestockRec.u_from_location + ','
+ movestockRec.u_from_bin + ','
+ movestockRec.u_to_loc + ','
+ movestockRec.u_to_bin + ','
+ movestockRec.u_disposition + ','
+ movestockRec.u_cost_centre + ','
+ movestockRec.u_reference + ','
+ movestockRec.u_reason_code + ','
+ movestockRec.u_notes + '\n';

2. Next is add an attachment to sys_attachment table and raise an event.

var att = new Attachment();
att.setFilename(current.number + '-Stock-Move-Batch-' + '.csv');
var id = att.attach();
gs.eventQueue(“incident.processed”, current, id);

3. Now in the email notification which is using “incident.processed” event, I have used below Notification Email Script to add attachment link on the email.

(function runMailScript(current, template, email, email_action, event) {
// Add your code here
function printattachments() {
var gr = new GlideRecord('sys_attachment');
while (gr.next()) {
template.print('Attachment: ' + gr.file_name + '\n');
})(current, template, email, email_action, event);

Add Images to approve/reject Incident resolution

We all know as end user we get this tacky mails every now and then on Incident resolution to Approve or Reject an incident. The hyperlink is quite boring and not 21st century enough! So I have created below mail script which can be embedded in Incident resolution mails to approve or reject using Thumbs Up or Thumbs Down. Very Facebooky! Check below mail script:

var image = "thumbs_up.png";
var response = "approve";
var mark = email.watermark;
var number = current.number;
var emailAddress = "icareITSupportdev@service-now.com";
//Different number if from an approval
if (current.getTableName().indexOf("sysapproval") != -1){
number = current.sysapproval.number;
var mailLink =

'<a href="mailto:'+ emailAddress + '?subject= Re:' + number + ' ' + response +'&body=%0A%0A' + mark + '"><img src="' + image + '" width="55" height="55" /></a>';


Just replace ‘thumbs_up.png’ image with a ‘thumbs_down.png’ image and set response to reject to create a ‘reject’ mail script.