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

Yes.  Depending on how the timing works, you might need to add a bit of a delay to check if the options are empty (since they change when the category field changes) but that should work.

I have been trying this with no success. It will make subcategory mandatory then leave it mandatory and not change back. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	  if (isLoading || newValue === '') {
      return;
   }
	
	if(newValue != oldValue){
	if(g_form.getValue("subcategory") == ""){//verify field names
		
		var f1 = g_form.getControl("subcategory");
		setTImeout(1000);
		
		if(f1.options.length != 1 ){ //check if the dropdown has choices
			g_form.setMandatory('subcategory',true);
			return false;
		}
		else{
			g.form.setMandatory('subcategory', false);
		}
	}
	}
	
}



Try this...

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (g_form.getValue("subcategory") == "") {//verify field names
        var f1 = g_form.getControl("subcategory");
        if (f1.options.length > 0) { //check if the dropdown has choices
            g_form.setMandatory('subcategory', true);
        }
        else {
            g.form.setMandatory('subcategory', false);
        }
    }
}

I found the issue and it's when I go to do f1.options.length it's grabbing the current subcat value before the subcat choices can be chnaged. So the previous choice list amount gets passed.

 

So should I use setTimeout(); and where should I put it? So I can slow down the script enough so it can grab the new choices. 

 

It seems that the code is moving quicker than the choice list can update and change 

You're right.  This should work better.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    setTimeout(setCatMandatory, 1000);
}

function setCatMandatory() {
    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);
        }
        else {
            g.form.setMandatory('subcategory', false);
        }
    }	
}