- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 01:15 AM
Hi All,
I have a requirement to add attachment in the emails.
I have created a mail script.
addAtachments();
function addAtachments() {
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
gr.orderByDesc('sys_created_on');
gr.query();
if (gr.next()) {
template.print('Attachment: <a href="https://' +
gs.getProperty('instance_name') +
'.service-now.com/sys_attachment.do?sys_id=' +
gr.sys_id + '">' + gr.file_name + '</a>\n');
}
}
But client wants to add attachment instead of link.
Please help in this.
Thanks,
Samiksha
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 05:10 AM
then to send latest file use this logic
1) create BR on sys_email table and use this script and it will send only latest file
I had used something similar in the past and it worked fine
After Insert on sys_email
(function executeRule(current, previous /*null when async*/) {
// Add your code here
try{
var att = new GlideRecord('sys_attachment');
att.orderByDesc("sys_created_on");
att.addQuery('file_name', fileName);
att.addQuery('table_name', 'sc_req_item');
att.addQuery('table_sys_id', current.instance);
att.setLimit(1);
att.query();
if(att.next()){
var rec = new GlideRecord('sys_email_attachment');
rec.initialize();
rec.attachment = att.sys_id;
rec.file_name = att.file_name;
rec.source = 'notification';
rec.content_disposition = 'attachment';
rec.email = current.sys_id;
rec.insert();
}
}
catch(ex){
gs.info('Email exception' + ex);
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 03:51 AM
please share what's the current setup along with screenshots.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 04:38 AM
Hi @Ankur Bawiskar ,
Please check the below SS:
and Mail script is add_attachment
addAtachments();
function addAtachments() {
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
gr.orderByDesc('sys_created_on');
gr.query();
if (gr.next()) {
template.print('Attachment: <a href="https://' +
gs.getProperty('instance_name') +
'.service-now.com/sys_attachment.do?sys_id=' +
gr.sys_id + '">' + gr.file_name + '</a>\n');
}
}
Whenever an attachment is added, in the additional comment we are updating attachment has been added.
Thanks,
Sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 05:10 AM
then to send latest file use this logic
1) create BR on sys_email table and use this script and it will send only latest file
I had used something similar in the past and it worked fine
After Insert on sys_email
(function executeRule(current, previous /*null when async*/) {
// Add your code here
try{
var att = new GlideRecord('sys_attachment');
att.orderByDesc("sys_created_on");
att.addQuery('file_name', fileName);
att.addQuery('table_name', 'sc_req_item');
att.addQuery('table_sys_id', current.instance);
att.setLimit(1);
att.query();
if(att.next()){
var rec = new GlideRecord('sys_email_attachment');
rec.initialize();
rec.attachment = att.sys_id;
rec.file_name = att.file_name;
rec.source = 'notification';
rec.content_disposition = 'attachment';
rec.email = current.sys_id;
rec.insert();
}
}
catch(ex){
gs.info('Email exception' + ex);
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 06:37 AM
Hi @Ankur Bawiskar ,
I created the BR. But in email attachment is not attached. I have remove the mail script. Now how I can send mail to requested for and watch list users?
Please guide me here.
Thanks,
Sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 06:42 AM
your requirement was related to attachments so I assume/believe this is already achieved -> Now how I can send mail to requested for and watch list users?
So here is what I assume based on your question
1) additional comment changes and email triggers
2) email script will pick latest file present on RITM record and send email with only this file (this is handled via the BR I shared)
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader