how to avoid inserting duplicate file name in sys_attachment table

Arun91
Tera Contributor

Hi Team,

We are using order guide and have created a variable as attachment. We are cascading the variable in servicenow.

Based on no of RITM attachments are inserted to sys_attachment table. Could you please help me how to avoid the duplicates while inserting the attachment into sys_attachment table.

5 REPLIES 5

Community Alums
Not applicable

Hi @Arun91 ,

This can be achieved by creating a before business rule on the sys_attachment table.

If there is a file with same name, same content type and of same size in bytes then the attachment upload is aborted.

The code for the same is as below

 
(function executeRule(current, previous /*null when async*/) {
var attach = new GlideRecord(‘sys_attachment’);
attach.addQuery(‘table_sys_id’, current.table_sys_id);
attach.addQuery(‘table_name’, current.table_name);
attach.addQuery(‘file_name’, current.file_name);
attach.addQuery(‘content_type’, current.content_type);
attach.addQuery(‘size_bytes’, current.size_bytes);
attach.query();
if(attach.next()){
current.setAbortAction(true);
}
})(current, previous);
 

HI,

If same file name we are using but content is different in that case it will aborted it. So it should compare both file name and content so that it will validate before inserting into sys_attachment table.

@Arun91  , Refer to this Community Link , Make some tweaks as per your requirement

 

https://www.servicenow.com/community/developer-forum/restrict-the-duplicate-file-attachment-to-a-inc...

 

Regards,

Shyamkumar

 

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

chetanb
Tera Guru

Hello @Arun91 ,

 

Can you please confirm you are on which version, seems issue has been fixed in recent version.

 

Regards,

CB