Prevent Duplicate Attachments insertion
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2018 02:13 PM
I have a requirement to display an alert or message when someone tries to insert a attachment of duplicate file name on some record. Can some one guide me how can this be achieved?
I have written a script on 'sys_attachment' table, to abort inserting a new record when some one tries to attach a file with same name of already existing one.
Here is the script:
(function executeRule(current, previous /*null when async*/) {
var iso_attach= new GlideRecord('sys_attachment');
iso_attach.addQuery('table_name', 'u_details');
iso_attach.addQuery('file_name', current.file_name);
iso_attach.addQuery('table_sys_id', current.table_sys_id);
iso_attach.query();
if(iso_attach.next()){
gs.addInfoMessage("hello");
current.setAbortAction(true);
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2018 11:06 AM
Hi, were you able to find a solution for this?? Do you mind sharing it? Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2018 12:00 AM
This is how we have done to restrict duplciate attachments.
Create a BEFORE BR rule on sys_attachment table on INSERT OR UPDATE
Select Advanced
and put this script. This basically reads the existing file having same table_sys_id and compares it with current file that is being uploaded. If it matches, then it aborts.
Script:
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',current.getValue('table_sys_id'));
gr.orderByDesc('table_sys_id');
gr.query();
while(gr.next()) {
if(gr.getValue('content_type').toLowerCase() == current.content_type.toLowerCase()) {
//cross verify the contents of this file with the existing file
var gr1 = new GlideRecord('sys_attachment');
gr1.get(gr.getValue('sys_id'));
var gsa = new GlideSysAttachment();
var binData = gsa.getBytes(gr1);
var strData = Packages.java.lang.String(binData);
//now read the current file contents as well
var gr2 = new GlideRecord('sys_attachment');
gr2.get(current.getValue('sys_id'));
var gsa1 = new GlideSysAttachment();
var binData1 = gsa1.getBytes(gr2);
var strData1 = Packages.java.lang.String(binData1);
//now compare the content of these 2 files. If equal, don't add again
if(strData == strData1) {
current.setAbortAction(true);
break;
}
}
}
})(current, previous);
Mark answer correct if you found this helpful

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-31-2019 02:24 AM
Hi Sangeetha, did you try the code that i shared above?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-31-2019 03:53 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-31-2019 10:15 AM
Hi Sangeetha,
The code that i have shared is part of BR and it shall not give you that error. I suspect that this error is due to something else. Kindly check the client scripts which are running and try to disable and check it.