Restrict the duplicate file attachment to a incident

Sri Harsha3
Tera Expert

Hi,

I'm a novice developer to service now. I have seen that an incident can have any number of attachments where they can also be a duplicate. There are multiple questions on the same issue, but most of the solutions are focused on the name based validation. I would like to do it using base64 in glidesystemattachment. Can someone guide me on how to do this?

 

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

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.

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Harsha,

You can have following approach:

1) onSubmit of form call script include and current record sys id

2) in script include check how many attachments are there of same type i.e. doc or pdf

3) get base64 encoded data and compare those.

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

asifnoor
Kilo Patron

Hi,

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.