Issue with Duplicate Attachment Copying from HR Task to HR Case

Satyam123
Tera Contributor

 

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

(function executeRule(current, previous /*null when async*/) {
// 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);

5 REPLIES 5

AmM01
Tera Contributor

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);



Satyam123
Tera Contributor

Screenshot 2025-04-14 at 2.03.56 AM.png

Screenshot 2025-04-14 at 2.04.13 AM.png

i have tried your BR but it is not working , can you refer the screenshots and let me know please

Ankur Bawiskar
Tera Patron
Tera Patron

@Satyam123 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

related list i am not getting properly , and it is not working