- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 02:46 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2022 04:50 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 02:56 AM
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);
}
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 03:16 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 03:04 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 03:20 AM
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