Avoid duplicate attachments in ServiceNow

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