Copy attachment from ritm to sc_task when added through native UI

aksh3
Giga Guru

Hello Experts,

I am facing the below issue as the image is getting duplicated. Below is my script which is written on sys_attchment table (after insert).

Please help .

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

    var ritmSysId = current.getValue('table_sys_id'); // Get RITM sys_id from attachment

    // Query all active sc_task records related to this RITM
    var grTask = new GlideRecord('sc_task');
    grTask.addQuery('request_item', ritmSysId);
    grTask.addQuery('active', true); // Only active tasks
    grTask.query();

    while (grTask.next()) {
        gs.log("Checking SC Task: " + grTask.number);

        var attachmentExists = false;
        var grAttachment = new GlideRecord('sys_attachment');
        grAttachment.addQuery('table_name', 'sc_task');
        grAttachment.addQuery('table_sys_id', grTask.sys_id);
        grAttachment.addQuery('file_name', current.file_name);
        grAttachment.query();

        if (grAttachment.next()) {
            attachmentExists = true;
        }

        if (!attachmentExists) {
            gs.log("Copying attachment to SC Task: " + grTask.number);
            GlideSysAttachment.copy('sc_req_item', ritmSysId, 'sc_task', grTask.sys_id);
        } else {
            gs.log("Attachment already exists in SC Task: " + grTask.number + " - Skipping.");
        }
    }

})(current, previous);



aksh3_0-1742286255855.png

aksh3_1-1742286276374.png

 

@Ankur Bawiskar 

Thanks.

 

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@aksh3 

if you use GlideSysAttachment.copy() it will copy all files again to target and not specific one

you can use this logic shared to copy single file

Copy unique attachments from case record to incident record 

Select specific attachments to copy 

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

Hi @Ankur Bawiskar ,

Can you please suggest the changes which i need to do in my script.

@aksh3 

I already shared the link for the same

Did you check that and make changes accordingly?

I believe you should be able to make the changes and make it work based on your developer skills.

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