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

ShawnM1
Kilo Contributor

Dave,

I like your method but how could you incorporate adding the pdf version of a latest published KB article to this?

Here is my use case...

I have a email notification to an external user that they have portal access with login instructions in the body of the email. We manually are attaching a pdf copy of a KB article in the request so it comes across the email as an attachment. It has detailed instructions with pictures and the works.

This is a repeatable step I can imagine would be easily automated. I'm not sure what you mean about "The attachment can be anywhere" or how it could fit my scenario?

 

Shawn

 

Swarup Patra
Kilo Guru

To send an email notification with a physical attachment in ServiceNow, you need to use the "MailScriptUtil" class. Here's how you can do it:

1. Create a new email notification.
2. In the "Advanced" tab, add the following script:

javascript
(function runMailScript(current, template, email, email_action, event) {
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', current.sys_id);
attachment.query();
while (attachment.next()) {
gs.print('Adding attachment: ' + attachment.name);
template.print('Adding attachment: ' + attachment.name);
email_action.addAttachment(attachment);
}
})(current, template, email, email_action, event);


This script will add all attachments related to the current record to the email notification.

Please note:

- Replace 'table_sys_id' with the sys_id of the record you want to attach files from.
- The 'current' object represents the record that triggered the email notification.
- The 'email_action' object is used to modify the email before it is sent.
- The 'template' object is used to modify the email template before it is applied to the email.
- The 'event' object represents the event that triggered the email notification.

This script will not work if the attachments are not related to the record that triggered the email notification. If you want to attach files from a different record, you need to modify the script accordingly.


nowKB.com
If you want to know any information about Service Now . Visit to https://nowkb.com/home