URGENT HELP- How to make sub-category mandatory only if it has dropdown.

ST9
Tera Contributor

Hi All,

How can i make a sub-category 2 field mandatory (which is dependent on sub-category 1 field) only when it has dropdown when the case is getting resolved.

I tried with UI policy but the problem is few of the sub-cat2 does not have any dropdown. So while using UI policy it will make sub-cat2 mandatory all the time even though it does not have any dropdown.find_real_file.png

 

1 ACCEPTED SOLUTION

1) You are using many brackets unnecessarily

2) You should pass sysid for assignment group reference fields

Update your script as below and pass sysids where ever I mentioned in script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 if (isLoading || newValue === '') {
return;
 }
if((newValue == '101') && ((g_form.getValue('assignment_group')=="sysid of group1")||(g_form.getValue('assignment_group')=="sysid of group2")||(g_form.getValue('assignment_group')=="sysid of group3")||(g_form.getValue('assignment_group'))=="sysid of group4"))
	
{
var subcat2 = g_form.getControl('u_sub_category_2'); 
var options = subcat2.options.length;

if(options > 1)
{
g_form.setMandatory('u_sub_category_2', true);
}
}
}

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

View solution in original post

17 REPLIES 17

Harish KM
Kilo Patron
Kilo Patron

Hi you can write onChange client script on Sub Category field

var subCat = g_form.getValue(sub_category);

alert(subCat);

if(subCat  == "resolved")

{

g_form.setMandatory("subcat2",true);

}

Regards
Harish

ST9
Tera Contributor

Hi Harish,

the problem is few of the sub-category2 does not have any dropdown since sub-category 2 is dependent on sub-category 1. So while using UI policy/ Client script, it was making sub-cat2 mandatory all the time even though it does not have any dropdown. It should be mandatory only when sub-cat2 has some dropdown

 

Martin Ivanov
Giga Sage
Giga Sage

Hey. Create onChange client script with the following code.

Replace with your table and field that you want to set non-mandatory.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	var field = g_form.getControl('hold_reason');// replace with your field
	var options = field.options.length;
	
	if(options==0){
		g_form.setMandatory(field, false);
	}
}

Please mark Correct and Helpful if my answer helps you resolve your issue. Thanks!
Martin Ivanov
Community Rising Star 2022


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

Hi Martin,

I tried you suggested code but still not working as per my requirement. The problem is few of the sub-category2 does not have any dropdown since sub-category 2 is dependent on sub-category 1. So while using UI policy/ Client script, it was making sub-cat2 mandatory all the time even though it does not have any dropdown. It should be mandatory only when sub-cat2 has some dropdown