Avoid duplicate attachments

Kim Kronborg
Kilo Expert

I have this before BR on sys_attachment table where I want to filter out duplicate attachments for the incident table. So if the user tries to add a file that's already present on the incident then it's not possible.

It seems to work on London release but not on Kingston - the manage attachments prompt just hangs when I activate the BR on Kingston.

Any suggestions to why it doesn't work on Kingston ?

Condition : current.table == 'incident'

Script:

var sysId = current.getValue('table_sys_id');

var fileName = current.getValue('file_name');

var foundDuplicate = false;

var gr = new GlideRecord('sys_attachment');

gr.addQuery('table_sys_id', sysId);

gr.query();

while (gr.next()){

     if (gr.getValue('file_name') === fileName){

         gs.log('Duplicate filename found in incident, action should be aborted');

         foundDuplicate = true;

     }

}

if (foundDuplicate){

    current.setAbortAction(true);

}

 

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kim,

What type of business rule is this? Is this before insert?

Regards

Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Omkar Mone
Mega Sage

Hi

 

Try this below in your before BR ;-

   var fname= new GlideRecord('sys_attachment');


                       fname.addQuery('table_sys_id', current.table_sys_id);


                       fname.addQuery('table_name', current.table_name);


                       fname.addQuery('file_name', current.file_name);


                       fname.addQuery('content_type', current.content_type);


                       fname.addQuery('size_bytes', current.size_bytes);


                       fname.query();


             if(fname.next()){


                         current.setAbortAction(true);
                         }

Hi Omkar,

Thanks for your suggestion - I tried your code in my before business rule but it had the same effect. The prompt just hangs and nothing happens. It seems like the abortaction is executed but the prompt is never closed after that.

It seems to work fine on London but not on Kingston.

Hi 

So maybe this is causing due to version issue. If you try with page reload after the abort action is performed you'll be suprised.

 

Anyways Thanks.