Copy attachment from ritm to sc_task when added through native UI
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2025 01:28 AM
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);
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2025 01:40 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2025 01:51 AM
Hi @Ankur Bawiskar ,
Can you please suggest the changes which i need to do in my script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2025 01:55 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader