How to Add latest attachment file to sys_email record from sys_attachment table

shaik11
Tera Expert

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

12 REPLIES 12

Saurabh Bhoi
Tera Contributor

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.

@Saurabh Bhoi 

Above code is not attaching single file to sys_email record.

 

Regards,

Reshma

I used that br and it works !

Ankur Bawiskar
Tera Patron
Tera Patron

@shaik11 

BR is on which table?

file is being attached on which table incident, problem etc?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader