We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

attachments copy from parent to child

Anshu56
Tera Contributor

I want to copy attachments from parent to child. I used the below script, but it's not working.
Before insert Business rule: 

(function executeRule(current, previous /*null when async*/) {
var parentAttachments = new GlideRecord('sys_attachment');
parentAttachments.addQuery('table_name', 'parent_table');
parentAttachments.addQuery('table_sys_id', current.parent.sys_id);
parentAttachments.query();

while (parentAttachments.next()) {
var childAttachment = new GlideSysAttachment();
childAttachment.copy(parentAttachments.getUniqueValue(), current.getUniqueValue());
}
})(current, previous);

3 REPLIES 3

Karan Chhabra6
Mega Sage

Hi @Anshu56 ,

 

The copy method accepts 4 parameters, please add table names as well, refer to the line below

childAttachment.copy('parent_table',parentAttachments.getUniqueValue(), 'child_table', current.getUniqueValue());

KaranChhabra6_0-1688368434123.png

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

 

I tried with this, but still not working

(function executeRule(current, previous /*null when async*/) {
var parentAttach = new GlideRecord('sys_attachment');
parentAttach.addQuery('table_name', 'parent_table');
parentAttach.addQuery('table_sys_id', current.parent.sys_id);
parentAttach.query();

while (parentAttach.next()) {
var childAttach = new GlideSysAttachment();
childAttach.copy('parent_table',parentAttach.getUniqueValue(), 'child_table', current.getUniqueValue());
}
})(current, previous);

 

Sagar Pagar
Tera Patron

Hi @Anshu56,

I would suggest to show the related attachments in related lists. instead of copy form parent to child. It will also avoid the duplicate attachments issues in ServiceNow.

 

Take a look at one of the best article that I saw and written by Jim. Modify it accordingly and use it.

TNT: "Related Attachments" Related List  

 

Thanks,

Sagar Pagar

The world works with ServiceNow