Copying Attachments fromSCTASK to RITM

Community Alums
Not applicable

I have written below BR to copy attachments from SCTASK to RITM but whenever I am attaching new file it is copying all existing articles in SCTASK to RITM.It need to copy only new file to RITM. Can you please help here

BR table name:sys_attachment & when to run "After insert"

var taskRec = new GlideRecord('sc_task');
    taskRec.addQuery('sys_id', current.table_sys_id);
    taskRec.setLimit(1);
    taskRec.query();
    if (taskRec.next()) {
        var ritmRec = new GlideRecord('sc_req_item');
        ritmRec.addQuery('sys_id', taskRec.request_item);
        ritmRec.addQuery('cat_item.name', 'catalog item name');
        ritmRec.setLimit(1);
        ritmRec.query();
        if (ritmRec.next()) {
            var grSourceAttachment = new GlideRecord('sys_attachment');
            grSourceAttachment.addQuery('table_sys_id', current.table_sys_id);
            grSourceAttachment.addQuery('table_name', 'sc_task');
            grSourceAttachment.query();
            while (grSourceAttachment.next()) {
                var sourceFileName = grSourceAttachment.getValue('file_name');
                var grTargetAttachment = new GlideRecord('sys_attachment');
                grTargetAttachment.addQuery('table_sys_id', taskRec.request_item);
                grTargetAttachment.addQuery('table_name', 'sc_req_item');
                grTargetAttachment.addQuery('file_name', sourceFileName);
                grTargetAttachment.query();
                if (!grTargetAttachment.hasNext()) {
                    var attachment = new GlideSysAttachment();
                    attachment.copy('sc_task', current.table_sys_id, 'sc_req_item', ritmRec.sys_id);
                }
            }
        }
    }

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums This is expected behaviour as the copy method of GlideSysAttachment() API copies all attachment from source record to destination record. 

 

copy(String sourceTable, String sourceID, String targetTable, String targetID)

Copies attachments from the source record to the target record.
 
 
 

Community Alums
Not applicable

Hello Team,
Is there any update on this or any other way we can avoid duplicate attachments