How to set 5 attachments mandatory in before insert business rule in for change request

Dasari Srihari
Tera Contributor

Hi All,

 

How to set 5 attachments mandatory in business rule. i

1 ACCEPTED SOLUTION

Kalyani Jangam1
Mega Sage
Mega Sage

Hi @Dasari Srihari 

Use Below code in before insert rule for change request

 

(function executeRule(current, previous /*null when async*/ ) {
 
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "change_request");
gr.addQuery("table_sys_id", current.sys_id);
gr.query();
var num = gr.getRowCount();
if (num < 5) {
gs.addErrorMessage("5 attachment File Mandatory for creating Change Request");
current.setAbortAction(true);
}
})(current, previous);
 
Please try and Mark Helpful and Correct if it really helps you.

View solution in original post

6 REPLIES 6

Kalyani Jangam1
Mega Sage
Mega Sage

Hi @Dasari Srihari 

Use Below code in before insert rule for change request

 

(function executeRule(current, previous /*null when async*/ ) {
 
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "change_request");
gr.addQuery("table_sys_id", current.sys_id);
gr.query();
var num = gr.getRowCount();
if (num < 5) {
gs.addErrorMessage("5 attachment File Mandatory for creating Change Request");
current.setAbortAction(true);
}
})(current, previous);
 
Please try and Mark Helpful and Correct if it really helps you.

Ankur Bawiskar
Tera Patron
Tera Patron

@Dasari Srihari 

you can use before insert BR on your table with this script

You need not query with table name; only table sysId is enough

You can also use GlideAggregate for optimization

UNDERSTANDING GLIDEAGGREGATE 

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

	var rec = new GlideRecord("sys_attachment");
	rec.addQuery("table_sys_id", current.getUniqueValue());
	rec.query();
	var count = rec.getRowCount();
	if (count < 5) {
		gs.addErrorMessage("Please attach 5 files");
		current.setAbortAction(true);
	}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar ,

 

Thanks for your response. I have 3 categories. if category = Test1 i need to set one attachment mandatory
category = Test2 i need 3 documents mandatory

Category = Test 10 i need set 10 documents mandatory. How we can implement this in BR

@Dasari Srihari 

just use if else and add the correct count check

I hope you will be able to enhance the below code as per your requirement by adding correct choice value for category and field name

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

	var rec = new GlideRecord("sys_attachment");
	rec.addQuery("table_sys_id", current.getUniqueValue());
	rec.query();
	var count = rec.getRowCount();

	var cat = current.category;
	if(cat == 'Test1' && count < 1){
		gs.addErrorMessage("Your Message 1");
		current.setAbortAction(true);
	}
	else if(cat == 'Test2' && count < 3){
		gs.addErrorMessage("Your Message 2");
		current.setAbortAction(true);
	}
	else if(cat == 'Test10' && count < 10){
		gs.addErrorMessage("Your Message 3");
		current.setAbortAction(true);
	}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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