How to Add latest attachment file to sys_email record from sys_attachment table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 02:34 AM
Hi All,
Actually, My task is to add latest created attachment file from "sys_attachment" table to "sys_email" record which we are creating automatically using Email Notification.
To attach latest attachment file are using 'after Business rule' to achieve it. But not working as expected, it is attaching multiple files to sys_email record.
Using Code:
(function executeRule(current, previous /*null when async*/) {
var gr= new GlideRecord('sys_attachment');
gr.addEncodedQuery('file_name=Servers.csv^table_name=sys_attachment');
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
if(gr.next())
{
var copyAtt = new GlideSysAttachment();
copyAtt.copy('sys_attachment', current.sys_id,'sys_email', current.sys_id);
}
})(current, previous);
Please provide inputs to achieve my requirement.
Regards,
Reshma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 02:47 AM
It seems like the issue is that the copy() method of the GlideSysAttachment API is being called without specifying the sys_attachment record to be copied. This is causing the copy() method to copy all the attachments associated with the sys_email record.
To fix this issue, you can modify the copyAtt.copy() method call to include the sys_attachment record to be copied by passing the sys_attachment record ID as the third parameter to the method. Here's the modified code:
(function executeRule(current, previous /*null when async*/) {
var gr= new GlideRecord('sys_attachment'); gr.addEncodedQuery('file_name=Servers.csv^table_name=sys_attachment');
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
if (gr.next()) {
var copyAtt = new GlideSysAttachment();
copyAtt.copy('sys_attachment', gr.sys_id, 'sys_email', current.sys_id);
} })(current, previous);
This should copy only the latest attachment to the sys_email record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 03:48 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 11:13 PM
I used that br and it works !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 03:58 AM
BR is on which table?
file is being attached on which table incident, problem etc?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader