attachments copy from parent to child
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2023 11:57 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2023 12:14 AM
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());
If my answer has helped with your question, please mark it as correct and helpful
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2023 01:27 AM - edited 07-03-2023 02:51 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2023 12:19 AM
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