Help with scripting mandatory fields if the field's choice list changes.

mitchmo2
Mega Expert

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;
}
}

}

1 ACCEPTED SOLUTION

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;
		}
	}
	
}

View solution in original post

15 REPLIES 15

Mark Stanger
Giga Sage

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.

Wouldn't I then have to have 2 scripts? One for category and subcategory onChange?

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!

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;
   }
 }