Making a field required based on another field question

Community Alums
Not applicable

I have a field on a form that the business would like to be required if another field is set to "Deploy". 
I think a client script is probably the correct route to go but would like to ask the question,

 

Which is the best way to make the field required if the status field changes to "deploy" and not required if it's not "deploy"?

Business rule?
UI Policy?

Client Script?

Again I'm leaning toward client script with an OnChange and I'll probably have to use an IF statement in there but could use some help with that if someone could offer some sample code.

1 ACCEPTED SOLUTION

Valmik Patil1
Kilo Sage

Hello ,

You can use below script in onChange client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    if(newValue == 2){ // instead of "2" give background value of your field
        g_form.setMandatory('assignment_group', true);  // your field name to make it mandatory
       
    }else{
        g_form.setMandatory('assignment_group', false); // your field name to make it non mandatory

    }
   
} 

 Also easy way to do it using UI policy with no code.

Thanks ,

Valmik Patil 

View solution in original post

5 REPLIES 5

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi @Community Alums ,

                              We always prefer OOB Configuration in ServiceNow. If its not possible with configuration then we can opt customization. So here in your case UI Policy is best practice to set field mandatory.

 

Kindly mark correct and helpful if applicable.