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

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

sumanta pal
Kilo Guru

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

1. First, you need to create a new instance of the GlideSysAttachment class.
2. Then, you can use the "write" method to attach a file to a record.
3. Finally, you can use the "addFile" method to add the attachment to the email.

Here's a sample script:

javascript
var gr = new GlideRecord('incident');
gr.get('incident_number', 'INC0010001'); // replace with your incident number

var attachment = new GlideSysAttachment();
var attachmentID = attachment.write(gr, 'filename.txt', 'text/plain', 'Hello, world!'); // replace with your file name, content type, and content

var email = new GlideEmailOutbound();
email.setSubject('Test email with attachment');
email.setBody('Please find the attached file.');
email.setTo('test@example.com'); // replace with your recipient email address
email.addFile(attachmentID);
email.send();


Please note:

- The "write" method takes four parameters: the record, the file name, the content type, and the content.
- The "addFile" method takes one parameter: the sys_id of the attachment.
- You need to replace the placeholders in the script with your actual values.
- This script should be run in a server-side script, such as a business rule or a script include.

For asking ServiceNow-related questions try this :

For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.

Please visit : https://nowkb.com/home
Our Website :https://nowkb.com/home
Link - https://nowgpt.ai/