Need to attach the last attached attachment to a notification record

kiran kumar m1
Tera Contributor

I added 4 attachments to a incident record , and when i moved the state to onhold the last attached attachment should get attached to a notification I created and it should trigger the notification.

 

for this I wrote a BR 
After Update
cond:-state changes to onhold

 

(function executeRule(current, previous /*null when async*/ ) {

        if (previous.state == current.state) return;


        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_name', 'incident');
        attachmentGR.addQuery('table_sys_id', current.sys_id);
        attachmentGR.orderByDesc('sys_created_on');
        attachmentGR.setLimit(1);
       
        attachmentGR.query();

        if (attachmentGR.next()) {

           

            var notifGR = new GlideRecord('sysevent_email_action');
            if (notifGR.get('ecb213b183c26210d8456ed0deaad33a')) {

                var attachment = new GlideSysAttachment();
                attachment.orderByDesc('sys_created_on');
                attachment.setLimit(1);
                attachment.copy('attachmentGR', current.sys_id, 'sysevent_email_action', notifGR.sys_id);


               
           
        }
    }

})(current, previous);
 
 
the problem is it is attaching all the attachments from incident to notification record 
Is there any other method where only the last attached can be attached to the notification record?
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@kiran kumar m1 

attachment.copy() will copy all files and you cannot specify only 1 file

check this link where I shared solution, please enhance

Adding latest attachment from sys_attachment table to sys_email record 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@kiran kumar m1 

attachment.copy() will copy all files and you cannot specify only 1 file

check this link where I shared solution, please enhance

Adding latest attachment from sys_attachment table to sys_email record 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@kiran kumar m1 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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