How to Avoid Duplicate Copy Attachment from Facilities Request to Facilities Task
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â01-07-2020 01:26 PM
Hello,
I need to copy attachment from Facilities Request (parent) to Facilities Task (child). I created a business rules on async for Insert and Update in Table Facilities Request Task with script GlideSysAttachment.copy('facilities_request', current.parent, 'facilities_request_task', current.sys_id);
It works fine when you attach the attachment file before submit. But if you attach the attachment file after submit, it created a duplication every time you add comment of update the facilities request task record. It duplicate the first file attachment when you add the second file attachment.
Please help on how to avoid the duplication files.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â01-07-2020 01:37 PM
Hi,
I think you need to add more validations in your script. Your business rule is for Insert/Update, and it is copying the attachments every time, that is why your are getting duplicates.
All the attachments go to "sys_attachment" table. One validation should be if the attachment exists, ignore it. If the attachment doesn't exist, copy it. In that way, you will have your sync between parent and child.
Hope this helps,
Pedro
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â01-07-2020 02:20 PM
Hi Pedro,
Thank you for your reply. I'm working on how to get the script to avoid duplication. If you have any sample scripts that could help me to solve the issue that would be great.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â01-07-2020 10:14 PM
Hi,
In your BR, before you execute the copy command, look into the glidesysattachment table if the record exists.
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id",current.sys_id);
gr.addQuery("table_name","facilities_request_task");
gr.query();
if(gr.getRowCount()==0) {
//add your copy script here
GlideSysAttachment.copy('facilities_request', current.parent, 'facilities_request_task', current.sys_id);
}
Mark the comment as a correct answer and helpful if it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â01-08-2020 03:08 AM
Hi,
If you want to restrict uploading duplicate files on the parent table (record). Write before insert BR on sys_attachment table.
var grAtt = new GlideRecord("sys_attachment");
grAtt.addQuery("table_sys_id",current.table_sys_id);
grAtt.addQuery("file_name",current.file_name);
grAtt.addQuery("table_name","facilities_request");
grAtt.query();
if(grAtt.getRowCount()>0) {
current.setAbortAction(true);
}