- 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 01:28 AM
is your notification table different than the table on which file is present?
if yes then 2 ways
1) send attachment link
OR
2) copy the attachment from that record to your record on which email will trigger, copy it just before email is sent
what's your use-case?
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:22 AM
Hi @Ankur Bawiskar ,
My use case is 2. When attachment is added in the requested item record, a mail will trigger will the attachment.(attachment not a link also the latest file). I have added link. how can I send attachment?
Thanks,
Samiksha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 03:27 AM
notification is on sys_attachment table?
If yes then directly use Include Attachment checkbox
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:45 AM
But @Ankur Bawiskar it will send all the attachment. I want to send only the latest attachment.
Thanks,
Sam