- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2024 07:02 AM
On Case record, we can attach attachments. But it should not be duplicate. If name is same & file type is same then attachment should not be attach to the record. It should show error message.
Thanks in advance,
Pooja Devkar
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2024 08:16 AM
Hi @Pooja M Devkar ,
You can have before insert BR on the sys_attachment table where table name is sn_customerservice_case, Loop through exising attachment name and content type.
2)
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var inc = new GlideRecord("sys_attachment");
inc.addQuery("table_sys_id", current.table_sys_id);
inc.query();
while(inc.next()){
if(current.file_name ==inc.file_name && current.content_type == inc.content_type){
gs.addErrorMessage("Attachment with same file name and type already exist");
current.setAbortAction(true);
}
}
})(current, previous);
Output:
Hope this helps!!
Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2024 08:16 AM
Hi @Pooja M Devkar ,
You can have before insert BR on the sys_attachment table where table name is sn_customerservice_case, Loop through exising attachment name and content type.
2)
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var inc = new GlideRecord("sys_attachment");
inc.addQuery("table_sys_id", current.table_sys_id);
inc.query();
while(inc.next()){
if(current.file_name ==inc.file_name && current.content_type == inc.content_type){
gs.addErrorMessage("Attachment with same file name and type already exist");
current.setAbortAction(true);
}
}
})(current, previous);
Output:
Hope this helps!!
Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2024 05:21 AM
Hi @Pooja M Devkar ,
did it work??
Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2024 09:39 AM
Hi @Pooja M Devkar ,
This is how we have done to restrict duplicate 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 as correct and helpful if this helps.
Thanks & Regards,
Sumanth Meda