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
‎02-06-2018 06:35 PM
Hi Sravanthi,
Write a Late before BR on attachment table for insert and update.
This will not allow to add duplicate attachment for a record.
Script:
function onBefore(current, previous) {
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);
}
}
Thank you,
VISHU
Mark answer correct if you found this helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2018 07:24 PM
Hi Vishu,
My script is working, i would also like to display an alert/message to user some thing like this :
"There is already File existing with the same name. Please change your uploading file name". How can this be done? If i add gs.addInfoMessage in the BR, no alert is coming up on the record.
Regards,
Sravanthi

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2018 07:36 PM
I think that's because you are running BR on sys_attachment table so the message will be displayed on that table only. Not sure how that can be possible since when we attach attachment suppose incident record then record doesn't get refreshed. if it would then we may think of something to do through client script (with GlideAjax call).
I was thinking if we can try something like let the 2nd duplicate attachment gets attached in the incident and when user update the incident record then let's query the sys_attachment table to find the latest attachment name and then compare the same name with other attachments name for the same incident record, if duplicate found then let's delete the latest attached duplicate attachment and throw a message about that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-30-2019 11:50 PM
Hi Vishal,
When I tried this I'm getting the log error as below and the Attachment popup box is not getting closed.
Exception while processing attachment: null: java.lang.NullPointerException: com.glide.ui.SysAttachmentInputStream.read(SysAttachmentInputStream.java:99)
javax.imageio.stream.MemoryCache.loadFromStream(MemoryCache.java:113)
javax.imageio.stream.MemoryCacheImageInputStream.read(MemoryCacheImageInputStream.java:110)
javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:351)
javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:361)
com.sun.imageio.plugins.png.PNGImageReader.readHeader(PNGImageReader.java:232)
com.sun.imageio.plugins.png.PNGImageReader.getWidth(PNGImageReader.java:1369)
com.glide.ui.SysAttachment.getImageDimensions(SysAttachment.java:2280)
com.glide.ui.SysAttachment.setImageDimensions(SysAttachment.java:2309)
com.glide.ui.SysAttachment.writeParts(SysAttachment.java:313)
com.glide.ui.SysAttachment.processRequest(SysAttachment.java:245)
com.glide.processors.AttachmentProcessor.process(AttachmentProcessor.java:76)
com.glide.processors.AProcessor.runProcessor(AProcessor.java:474)
com.glide.processors.AProcessor.processTransaction(AProcessor.java:199)
com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:178)
com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:167)
com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:31)
com.glide.sys.Transaction.run(Transaction.java:2037)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)