- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2018 02:29 PM
What I have set up here is when there are options in a choice list based upon the previous field choice then that field should become mandatory. So the user has to fill it in. Right now I am running this on onSubmit command, but when I change the choice list in the first field to something that doesn't have any other options in the other 2 fields, the fields stay mandatory even though there are no longer any options to choose from. Any ideas how I can modify my code so where the fields will be mandatory only if there are options in the fields?
I'm guessing I need to put g_form.setMandatory('subcategory',false); g_form.setMandatory('u_subcategory_2',false); somewhere.
https://community.servicenow.com/community?id=community_question&sys_id=eed5f94adb382f848e7c2926ca961917
Here's a link to my previous post about this to explain the category situation I am trying to set up.
function onSubmit() {
if(g_form.getValue("subcategory") == ""){//verify field names
var f1 = g_form.getControl("subcategory");
if(f1.options.length != 1 ){ //check if the dropdown has choices
g_form.setMandatory('subcategory',true);
g_form.addErrorMessage("The following mandatory fields are not filled in: Subcategory");
return false;
}
}
if(g_form.getValue("u_subcategory_2") == ""){//verify field names
var f2 = g_form.getControl("u_subcategory_2");
if(f2.options.length != 1 ){ //check if the dropdown has choices
g_form.setMandatory('u_subcategory_2',true);
g_form.addErrorMessage("The following mandatory fields are not filled in: Subcategory2");
return false;
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-26-2018 01:59 AM
Hey I ended up doing this: Thank you for the help
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
g_form.setMandatory('subcategory', false);
return;
}
var myVar;
myVar = setTimeout(alertFunc, 400);
}
function alertFunc() {
if(g_form.getValue("subcategory") == ""){//verify field names
var f1 = g_form.getControl("subcategory");
if(f1.options.length != 1 ){ //check if the dropdown has choices
g_form.setMandatory('subcategory',true);
return;
}
else{
g_form.setMandatory('subcategory', false);
return;
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2018 03:39 PM
I think you'd be better off running this in an 'onChange' client script instead. Each time the 'Category' or 'Subcategory' field changes, add or remove the options, then check the option length, and then make the field mandatory or not using an if/else statement. I think the problem right now is that you never have anything that makes the fields not mandatory. That's hard to do in an 'onSubmit' script because mandatory checks happen before the script runs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-12-2018 07:26 AM
Wouldn't I then have to have 2 scripts? One for category and subcategory onChange?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-12-2018 07:29 AM
Yes, you would need to have one 'onChange' script for each field.
Please mark my response above correct if I've answered your question. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-12-2018 08:27 AM
So I would do something like this on the category field for onChange
if(g_form.getValue("subcategory") == ""){//verify field names
g.form.setMandatory('subcategory',false);
var f1 = g_form.getControl("subcategory");
if(f1.options.length != 1 ){ //check if the dropdown has choices
g_form.setMandatory('subcategory',true);
g_form.addErrorMessage("The following mandatory fields are not filled in: Subcategory");
return false;
}
}