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
‎01-28-2020 05:54 AM
Hey!!
How did you get this error in your incident form?
I am trying to get it but not sure why it's not coming in my Incident form.
I wrote before BR in the attachment table.
Thank you in advance.
Regards,
Priya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2020 06:06 AM
Hi Vinodparam,
Can you please share the code for this. I was able to abort the attachment, but i can not able to show the error message as you did, please help me with that code. Thank you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2020 07:05 AM
Hello Sravs,
Please try the below script to prevent duplicate attachment
function onSubmit()
{
var id = gel('sysparm_item_guid').value;
var arr = [];
var grattach = new GlideRecord("sys_attachment");
grattach.addQuery("table_name","sc_cart_item");
grattach.addQuery("table_sys_id",id);
grattach.query();
while(grattach.next())
{
alert('Duplicate attachment not allowed);
return false;
}
}
Please Mark it helpful/correct if my answer helps in any way to resolve your query.
Reach out to me if any more help required.
Regards
Yash.K.Agrawal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2024 03:23 AM
Hi All,
Came across this older post and wanted to post an updated solution in case it helps someone. My requirement was to prevent attaching an attachment to a record if there is already a record with that name on the record. So instead of looking at duplicate names across the entire table, I am just checking against the individual record where you are trying to add the attachment. For example, you can have two files called image1.jpg on two different incidents but not on the same incident.
- Create a BR on sys_attachment
- When = Before
- Insert = true
- Script (see below)
(function executeRule(current, previous /*null when async*/) {
// Get the table and sys_id of the record the attachment is associated with
var tableName = current.table_name;
var tableSysId = current.table_sys_id;
var attachmentName = current.file_name;
// GlideRecord to query attachments to find any duplicates
var grAttach = new GlideRecord('sys_attachment');
grAttach.addQuery('table_name', tableName);
grAttach.addQuery('table_sys_id', tableSysId);
grAttach.addQuery('file_name', attachmentName);
grAttach.query();
// If a duplicate, abort and display error message
if (grAttach.next()) {
gs.addErrorMessage('Attachment name already exists on this record.');
current.setAbortAction(true);
}
})(current, previous);