We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to send email notification with physical attachment

Rameshnathan
Tera Expert

How to send email notification with physical attachment

I have used this below script in email script notification, but its sending the attachment as link but i want to send real  attachment in email please guide me how to do

notification

var eve = "93e903b1db505700d7fa5040cf9619e4";
var gr =new GlideRecord('sys_attachment');
gr.addQuery('sys_id','IN',eve);
gr.query();
while(gr.next()){
template.print('Attachment: <a href="http://'+gs.getProperty("instance_name")+'.service-now.com/sys_attachment.do?sys_id='+ gr.sys_id + '">' + gr.file_name + '</a>\n');
}

21 REPLIES 21

Hi Drew, Really helpful.Its working for me like a charm. Thanks

Thanks Drew. Exactly what the Dr. ordered.

Hey Drew,

This code is really helpful. Thanks alot! However, I am facing an issue with the attachment name in the record (RITM). The name doesn't show up and when clicked, it gets downloaded as 'sys_attachment' (file name is 'How to configure') even when the proper file name is given in the script. 

Any idea what could be the reason? Thanks in Advance!

cloudminus89
Mega Contributor

I got around the issue of attachments link not working for non roled users by 

creating a knowledge article and adding an attachment to it,

publishing it,

then referencing its attachment from the email notification script

 

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

  var url='/sys_attachment.do?sys_id=1d9ab0f31b5db300cee5ed7cee4bcb08&sysparm_viewer_table=kb_knowledge&sysparm_viewer_id=497a30f31b5db300cee5ed7cee4bcb81'; //--had to add the attachment to a KB article in order to be able to allow non-roled users to access the attachment


  template.print ('<a href="' + url + '">Click to download the floor plan</a>');


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

Dave129
Kilo Contributor

My notification is being triggered by an event that's generated by an RITM workflow. The attachment can be anywhere. I set up a 'before insert' business rule on the sys_email table:

Condition:

current.instance.cat_item == 'sys_id of the catalog item' && current.subject == 'my subject'

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var grSysAtt = new GlideRecord('sys_attachment');
	grSysAtt.get('sys_id of the attachment');
	var content = new GlideSysAttachment().getContentStream(grSysAtt.sys_id);
    new GlideSysAttachment().writeContentStream(current, grSysAtt.getValue('file_name'), grSysAtt.getValue('content_type'), content);
})(current, previous);