Issue with Duplicate Attachment Copying from HR Task to HR Case
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2025 12:23 PM
I have created a business rule that uses the insert and update actions to copy attachments from an HR Task to an HR Case. However, I am still encountering issues with duplicate attachments being copied. Additionally, attachments are only being copied from the HR Task to the HR Case when I comment on the HR Task.
I want to add the attachment so that whenever I attach any document in the HR Task, it automatically sends it to the parent HR Case, only the newly added doc not all the previous one.
Thanks
// Ensure attachments are only copied once
var hrCase = new GlideRecord('sn_hr_core_case');
if (hrCase.get(current.parent.sys_id) && !hrCase.attachments_copied) {
// Copy new attachments from HR Task to HR Case (you may have a specific function for this)
new hr_Task().updateAttachments(current);
// Set the flag to prevent re-copying
hrCase.attachments_copied = true;
hrCase.update();
}
})(current, previous);
- Labels:
-
Human Resources Service Delivery
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2025 11:18 PM
I believe copying the attachments won't be the best approach, as copying files would definitely increase the table size of sys_attachment.
However, there is a better way to do this, please check the below article.
"Related Attachments" Related List
Check this one too:
Copy Attachment - Trick and Treat! (Low Code Solut... - ServiceNow Community
However, if you insist on implementing your approach, please check the below recommendation:
- Use a Business Rule on sys_attachment table (as I mentioned earlier).
Table: sys_attachment
When: after insert
Condition:
current.table_name == 'sn_hr_core_task'
Script:
(function executeRule(current, previous /*null when async*/) {
if (current.table_name !== 'sn_hr_core_task') return;
var taskGR = new GlideRecord('sn_hr_core_task');
if (taskGR.get(current.table_sys_id) && taskGR.parent) {
var caseSysId = taskGR.parent.toString();
var saUtil = new GlideSysAttachment();
saUtil.copy('sn_hr_core_task', taskGR.sys_id, 'sn_hr_core_case', caseSysId, current.sys_id);
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2025 01:35 PM
i have tried your BR but it is not working , can you refer the screenshots and let me know please
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2025 06:39 AM
I recommend not storing attachments again on other table as it will increase the attachment table size and will lead to performance issue
Rather check this link on how to show the attachment in related list
TNT: "Related Attachments" Related List
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
04-15-2025 05:15 AM
related list i am not getting properly , and it is not working