Prevent Duplicate Attachments insertion

sravs99999
Mega Contributor

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

18 REPLIES 18

Sangeetha12
Kilo Contributor

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)

 

 

 

Gagan10
Giga Contributor

Create BR on sys_attachment table , find_real_file.png

 

 

find_real_file.png

Hi Gagan Deep,
 
Already I'm using the same way as u mentioned above. 
 
I'm not aware of file attachment of OOB but in my case, the file attachment is done by auto insert (On selection on file it's getting inserted.)
 
After using the BR file is not getting inserted but the attachment popup box is still remaining it's not getting closed. and in log getting a null value error (Exception while processing attachment: null: java.lang.NullPointerException:)
 

vinodparam
Kilo Explorer

Yes this works. I have an additional requirement: The above script prevents the users to upload duplicate files and I can display an error message. But the file still shows up in the attachments list until the record is refreshed (attached screenshot). Is there any way to prevent this?

find_real_file.png

Hi,

Use gs.setRedirect to the record so that the page gets loaded on error.

Mark the comment as helpful if this helps.