Add attachment to outbound email
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2017 04:46 PM
Hi all,
I want to trigger an outbound email when an attachment is added to a particular type of task, and I want the attachment to be included in the email (but only the latest attachment that's just been added to the task - NOT all attachments that may have been added previously as well).
I have a business rule that runs on the sys_attachment table itself, with a condition that specifies the particular type of task. The business rule generates an event that triggers the email notification. So, in the context of that email notification, 'current' passed in on the event is actually the attachment record itself. Is there a way I can script to include that specific attachment in the outbound email that gets generated?
Thanks for any pointers,
Jamie.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2017 08:35 PM
Jamie,
Check out other post. Add attachment to Notification Email
Use a mail script to do below code. (but use your event to pass in the sys_id of the attachment file that was added)
dothis();
function dothis() {
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',current.sys_id);
gr.query();
while (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>');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 02:19 AM
Hello Patrick,
Thank you for this reply, but I should have specified- I would like to attach the actual file to the outbound email, rather than a hyperlink.
Jamie.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 12:39 PM
Jamie,
Why doesn't the link within the email work for your needs?
The only way I know how to get attachment to be attached to email is to somehow copy over the attachment to the sys_email record for the outbound email message.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 03:33 PM
Hi Patrick,
This is part of an email integration with a partner's system. They want us to send the actual file so that it can be attached to the linked task in their system. They won't be accessing our ServiceNow instance.
But I went down the route you've suggested and wrote a business rule on the sys_email table to copy the record on the sys_attachment table, changing the table_name and table_sys_id to associate the attachment with the outbound email on the sys_email table:
var attchmt = new GlideRecord('sys_attachment');
attchmt.get(current.instance);
attchmt.table_name = 'sys_email';
attchmt.table_sys_id = current.sys_id;
attchmt.insert();
As the email notification is triggered by an event on the sys_attachment table, I found that 'current.instance' directly references the attachment, which is handy.
It looked like this had worked - I can see a new record on the sys_attachment table, with revised values for table_name and table_sys_id. But, although a file was attached to the outbound email, and it had the right name, the file itself was empty and couldn't be opened. I'm not sure what's going on with this!
But if I change line 05 in the code to attchmt.update(), to update the sys_attachment record rather than copy it, I found it worked - and the outbound email included the attachment, NOT as an empty file. The only issue now was that the attachment had disappeared from the record it was originally attached to. So, I wrote a second business rule that runs when the email has been sent, to move the attachment back to the original record. It was a bit convoluted as I had to hop from sys_email back through the event to identify the original record the attachment was added to, but it works. The only remaining issue is that although the email has been sent and received, with the attachment successfully included, in the email log the attachment is NOT visible against the email. Still, I'm satisifed with this.
Jamie.