Copying Attachments fromSCTASK to RITM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2024 12:40 AM
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);
}
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2024 03:35 AM
@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)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2024 03:27 AM
Hello Team,
Is there any update on this or any other way we can avoid duplicate attachments

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2024 03:40 AM
Hi @Community Alums
Please follow these threads for copy Attachments
Kindly mark my answer as Correct and helpful based on the Impact.