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.

Else if not working in client script for state field

rmaroti
Tera Contributor

when state == null then no one reason field will be display.

 

could you please check below code:

else if(state == '')
{
g_form.setMandatory('u_in_progress_reason',false);
g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_new_reason',false);
g_form.setDisplay('u_new_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_on_hold_reason',false);
g_form.setDisplay('u_on_hold_reason', false);
g_form.setMandatory('u_closed_reason',false);
g_form.setDisplay('u_closed_reason', false);
g_form.setMandatory('u_resolved_reason',false);
g_form.setDisplay('u_resolved_reason',false);
}

8 REPLIES 8

Anurag Tripathi
Mega Patron
Mega Patron

Firstly, you can do this easily using a ui policy with conditions form the filters, that is the recommended way for managing field behavior

 

To check for null you can do something like 

 

if(g_form.getValue('state))
{
//do somehting 
}
else
{ //this is the null block
g_form.setMandatory('u_in_progress_reason',false);
g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_new_reason',false);
g_form.setDisplay('u_new_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_on_hold_reason',false);
g_form.setDisplay('u_on_hold_reason', false);
g_form.setMandatory('u_closed_reason',false);
g_form.setDisplay('u_closed_reason', false);
g_form.setMandatory('u_resolved_reason',false);
g_form.setDisplay('u_resolved_reason',false);
}

 

-Anurag

Harsh_Deep
Giga Sage
Giga Sage

Hello @rmaroti 

 

Can you please send me whole code

 

Thanks

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
//     g_form.setDisplay('u_new_reason', false);
//     g_form.setDisplay('u_cancelled_reason', false);
//     g_form.setDisplay('u_resolved_reason', false);
//     g_form.setDisplay('u_on_hold_reason', false);
//     g_form.setDisplay('u_closed_reason', false);
//    g_form.setDisplay('u_in_progress_reason', false);
 
   var state = g_form.getValue('u_state');
 
if(state  == 1)
{
g_form.setMandatory('u_new_reason',true);
g_form.setDisplay('u_new_reason',true);
g_form.setMandatory('u_on_hold_reason',false);
g_form.setDisplay('u_on_hold_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_in_progress_reason',false);
            g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_closed_reason',false);
            g_form.setDisplay('u_closed_reason', false);
        g_form.setMandatory('u_resolved_reason', false);
g_form.setDisplay('u_resolved_reason',false);
 
}
 
else if(state  == 2)
{
   
g_form.setMandatory('u_in_progress_reason',true);
g_form.setDisplay('u_in_progress_reason',true);
g_form.setMandatory('u_new_reason',false);
g_form.setDisplay('u_new_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_on_hold_reason',false);
            g_form.setDisplay('u_on_hold_reason', false);
g_form.setMandatory('u_closed_reason',false);
            g_form.setDisplay('u_closed_reason', false);
        g_form.setMandatory('u_resolved_reason', false);
g_form.setDisplay('u_resolved_reason',false);
 
}
   else if(state  == 3)
{
g_form.setMandatory('u_on_hold_reason',true);
g_form.setDisplay('u_on_hold_reason',true);
g_form.setMandatory('u_new_reason',false);
g_form.setDisplay('u_new_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_in_progress_reason',false);
            g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_closed_reason',false);
            g_form.setDisplay('u_closed_reason', false);
        g_form.setMandatory('u_resolved_reason', false);
g_form.setDisplay('u_resolved_reason',false);
}
 
else if(state  == 6)
{
g_form.setMandatory('u_resolved_reason',true);
g_form.setDisplay('u_resolved_reason',true);
g_form.setMandatory('u_on_hold_reason',false);
g_form.setDisplay('u_on_hold_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_in_progress_reason',false);
            g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_closed_reason',false);
            g_form.setDisplay('u_closed_reason', false);
        g_form.setMandatory('u_new_reason', false);
g_form.setDisplay('u_new_reason',false);
 
}
else if(state  == 7)
{
g_form.setMandatory('u_closed_reason',true);
g_form.setDisplay('u_closed_reason',true);
g_form.setMandatory('u_on_hold_reason',false);
g_form.setDisplay('u_on_hold_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_in_progress_reason',false);
            g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_new_reason',false);
            g_form.setDisplay('u_new_reason', false);
        g_form.setMandatory('u_resolved_reason', false);
g_form.setDisplay('u_resolved_reason',false);
}
else if(state  == 😎
{
g_form.setMandatory('u_cancelled_reason',true);
g_form.setDisplay('u_cancelled_reason',true);
g_form.setMandatory('u_on_hold_reason',false);
g_form.setDisplay('u_on_hold_reason',false);
g_form.setMandatory('u_new_reason',false);
g_form.setDisplay('u_new_reason', false);
g_form.setMandatory('u_in_progress_reason',false);
            g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_closed_reason',false);
            g_form.setDisplay('u_closed_reason', false);
        g_form.setMandatory('u_resolved_reason', false);
g_form.setDisplay('u_resolved_reason',false);
}
else if(state == '')
{
g_form.setMandatory('u_in_progress_reason',false);
g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_new_reason',false);
g_form.setDisplay('u_new_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_on_hold_reason',false);
            g_form.setDisplay('u_on_hold_reason', false);
g_form.setMandatory('u_closed_reason',false);
            g_form.setDisplay('u_closed_reason', false);
        g_form.setMandatory('u_resolved_reason',false);
g_form.setDisplay('u_resolved_reason',false);
}
}

rmaroti
Tera Contributor
else if(state == '')
{
g_form.setMandatory('u_in_progress_reason',false);
g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_new_reason',false);
g_form.setDisplay('u_new_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_on_hold_reason',false);
            g_form.setDisplay('u_on_hold_reason', false);
g_form.setMandatory('u_closed_reason',false);
            g_form.setDisplay('u_closed_reason', false);
        g_form.setMandatory('u_resolved_reason',false);
g_form.setDisplay('u_resolved_reason',false);
}
 
 
this last else if is  not working