Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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