How to limit the total number of attachments in a form like incident?

Rohith1
Mega Contributor

How to limit the total number of attachments on a form like incident i.e., For Example the maximum number of attachments a user can upload should be 5 and not more than that.

4 REPLIES 4

shloke04
Kilo Patron

Hi,

 

This can be done by writing a Before Insert Business rule on sys_attachment table as mentioned below:

 

Condition of the Business Rule: Run it only for the table you want this functionality for. For example:

current.table_name == 'xyz'   //Replace xyz with your Table Name

 

(function executeRule(current, previous /*null when async*/) {
	
	// Add your code here
	if(current.table_name == 'incident'){
		var attRec = new GlideRecord('sys_attachment');
		attRec.addQuery('table_sys_id', current.table_sys_id);
		attRec.query();
		while(attRec.next()){
			var getCount = attRec.getRowCount();
			gs.addInfoMessage(getCount);
			if(getCount > 1){
				gs.addErrorMessage('Maximum number of attachments allowed are 1');
				current.setAbortAction(true);
				
			}
		}
	}
	
})(current, previous);

 

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

 

Regards,

Shloke

 

 

 

 

 

 

 

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Rohith1
Mega Contributor

actually i've written similar code but still it is not working

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Rohith,

use below script and show message to user

use before insert business rule on sys_attachment table; with condition as current.table_name == 'incident'

script below

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.table_sys_id);
gr.query();
var rowCount = gr.getRowCount();

if(rowCount > 5){
gs.addErrorMessage('Only 5 attachments are allowed');
current.setAbortAction(true);
}

screenshot below

find_real_file.png

find_real_file.png

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

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

No it is not working