How to Avoid Duplicate Copy Attachment from Facilities Request to Facilities Task

sl888
Kilo Guru

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.

 

5 REPLIES 5

Pedro Lopez
Kilo Guru

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

sl888
Kilo Guru

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.

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.

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