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');
}

22 REPLIES 22

Hi Drew,

Could you please share the screenshot?

I tried using your code but no attachment got attached to my triggered email.

We have only one attachment to be attached in email notification so i have commented if condition.

 

Below is the code could you please correct me.

 

var myStringArray = "288ed2c4db333300a4798742399619fd";

//This field wasn't used so I commented it out
//var att =[];


var sys = current.instance;
//var sys = " 8f9389b5c0a8016401715c208ff9bf48";
gs.log("test"+sys);


var grSysAtt = new GlideRecord('sys_attachment');
grSysAtt.addQuery('table_sys_id',sys);
grSysAtt.query();
while(grSysAtt.next()) {
var asys = grSysAtt.sys_id;
gs.log('test 2'+asys);


//if(myStringArray.indexOf(asys)>=0){
var content = new GlideSysAttachment().getContentStream(grSysAtt.sys_id);
new GlideSysAttachment().writeContentStream(current, grSysAtt.getValue('file_name'), grSysAtt.getValue('content_type'), content);
gs.log("test name" + grSysAtt.getValue('file_name'));
//}
}

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

 

Thanks and Regards,

Meenal

Hey Meenal,

I have the sys variables commented differently than yours.  Try flipping the comment and see if that works.

//var sys = current.instance;
var sys = " 8f9389b5c0a8016401715c208ff9bf48";

Dave129
Kilo Contributor

When I use this, it attaches the file to the record that is sending the notification (which makes sense, with 'current' being specified in 'writeContentStream'), not the notification email.

For example, my notification is triggered from a workflow tied to a requested item. When the notification fires, this script attaches the file to the requested item.

Edit: I got it to work. You have to make sure "include attachments" is checked on the notification "what it will contain" tab. So, the script copies the attachment to the originating record, and "include attachments" makes the notification grab files that are attached to the record. Sort of a weird way to do it, but it works.

Thanks, enabling "include attachments" made it work, but it also included all other attachments from the RITM. I think this will be fine for now.

Hey Dave,

I tried Drew's code and also checked Include Attachments in the notification. Still the file didn't attach to the email on Outlook. Any suggestions what could be the possible reason?

Thanks In Advance!