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

Ilo Gassoway
Mega Guru

Depending on what you're trying to accomplish a UI Policy may be an easy, non scripting option 

I was going to do this, but if a chosen value in the category field has options in the subcategory field I am trying to make subcat mandatory and do the same thing if subcategory creates options in subcategory2. I don't think this is possible with a UI policy. 

Understood.

@Gassoway is correct if you have only a few fields you need to change - otherwise the script is the way to go.   even in 2021 SNOW is very non-productive when it comes to performing actions on MANY variables/fields within the gui. the one thing i despise most about SNOW is the lack of performing one action across many items. 

additionally @mark is correct that the OnChange is best because OnSubmit is waiting for you to submit.

Pranay Tiwari
Kilo Guru

Hi ,

try below script,it will work perfectly..

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	setTimeout(setCatMandatory, 1000);}
	
	
	function setCatMandatory() {
		if (g_form.getValue("u_choice_2") == "") {//verify field names
			var f1 = g_form.getControl("u_choice_2");
			
			if (f1.options.length<2) {
				g_form.setMandatory('u_choice_2',false);
			}
			else {
				g_form.setMandatory('u_choice_2',true);
			}
		}
		
	}

 

 

Mark correct or helpful if it helps you.

 

Warm Regards,

Pranay Tiwari

| www.DxSherpa.com | pranay.tiwari@dxsherpa.com |