How to check the attachment is already attached to Case record?

Pooja M Devkar
Tera Contributor

On Case record, we can attach attachments. But it should not be duplicate. If name is same & file type is same then attachment should not be attach to the record. It should show error message.

 

Thanks in advance,

Pooja Devkar

1 ACCEPTED SOLUTION

Hemanth M1
Giga Sage
Giga Sage

Hi @Pooja M Devkar ,

 

You can have before insert BR on the sys_attachment table where table name is sn_customerservice_case, Loop through exising attachment name and content type.

 

HemanthM1_0-1722438697058.png

 

2)

HemanthM1_1-1722438740958.png

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var inc = new GlideRecord("sys_attachment");
	inc.addQuery("table_sys_id", current.table_sys_id);
	inc.query();
	while(inc.next()){
		if(current.file_name ==inc.file_name && current.content_type == inc.content_type){
			gs.addErrorMessage("Attachment with same file name and type already exist");
			current.setAbortAction(true);
		}
	}

})(current, previous);

 

Output:

HemanthM1_2-1722438942947.png

 

Hope this helps!!

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

View solution in original post

3 REPLIES 3

Hemanth M1
Giga Sage
Giga Sage

Hi @Pooja M Devkar ,

 

You can have before insert BR on the sys_attachment table where table name is sn_customerservice_case, Loop through exising attachment name and content type.

 

HemanthM1_0-1722438697058.png

 

2)

HemanthM1_1-1722438740958.png

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var inc = new GlideRecord("sys_attachment");
	inc.addQuery("table_sys_id", current.table_sys_id);
	inc.query();
	while(inc.next()){
		if(current.file_name ==inc.file_name && current.content_type == inc.content_type){
			gs.addErrorMessage("Attachment with same file name and type already exist");
			current.setAbortAction(true);
		}
	}

})(current, previous);

 

Output:

HemanthM1_2-1722438942947.png

 

Hope this helps!!

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Hi @Pooja M Devkar ,

 

did it work??

 

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Sumanth16
Kilo Patron

Hi @Pooja M Devkar ,

 

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.

 

Thanks & Regards,

Sumanth Meda