None option for a drop down

Community Alums
Not applicable

Hi,

How to change the code for the None option to work? The rest of the options are good.

 

1.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   var status = g_form.getValue('u_state');
   if(status == 'new'){
	g_form.setMandatory('u_in_progress_status', false);
	g_form.setMandatory('u_resolved_status', false);
	g_form.setMandatory('u_canceled_status', false);
	g_form.setVisible('u_new_status', true);
	g_form.setMandatory('u_new_status', true);
	g_form.setDisplay('u_in_progress_status', false);
	g_form.setDisplay('u_resolved_status', false);
	g_form.setDisplay('u_canceled_status', false);
   }
   if(status == 'in_progress'){
	g_form.setMandatory('u_new_status', false);
	g_form.setMandatory('u_resolved_status', false);
	g_form.setMandatory('u_canceled_status', false);
	g_form.setVisible('u_in_progress_status', true);
	g_form.setMandatory('u_in_progress_status', true);
	g_form.setDisplay('u_new_status', false);
	g_form.setDisplay('u_resolved_status', false);
	g_form.setDisplay('u_canceled_status', false);
   }
   if(status == 'resolved'){
	g_form.setMandatory('u_new_status', false);
	g_form.setMandatory('u_in_progress_status', false);
	g_form.setMandatory('u_canceled_status', false);
	g_form.setVisible('u_resolved_status', true);
	g_form.setMandatory('u_resolved_status', true);
	g_form.setDisplay('u_new_status', false);
	g_form.setDisplay('u_in_progress_status', false);
	g_form.setDisplay('u_canceled_status', false);
   }
   if(status == 'Canceled'){
	g_form.setMandatory('u_new_status', false);
	g_form.setMandatory('u_in_progress_status', false);
	g_form.setMandatory('u_resolved_status', false);
	g_form.setVisible('u_canceled_status', true);
	g_form.setMandatory('u_canceled_status', true);
	g_form.setDisplay('u_new_status', false);
	g_form.setDisplay('u_in_progress_status', false);
	g_form.setDisplay('u_resolved_status', false);
   }
//    if((status != 'new') && (status != 'in_progress') && (status != 'resolved') && (status != 'Canceled'){
// 	g_form.setMandatory('u_new_status', false);
// 	g_form.setMandatory('u_in_progress_status', false);
// 	g_form.setMandatory('u_resolved_status', false);
// 	g_form.setMandatory('u_canceled_status', false);
// 	g_form.setDisplay('u_in_progress_status', false);
// 	g_form.setDisplay('u_resolved_status', false);
// 	g_form.setDisplay('u_canceled_status', false);
// 	g_form.setDisplay('u_new_status', false);
//    }
   
}

 

2.png

 

3.png

 

In the picture you can see the New status is not getting removed when I change the status to None.

 

Regards

Suman P.

6 REPLIES 6

Hello @Community Alums 

Servicenow recommend to use ootb functionality and follow low code approach, just for improving the skills in scripting replace the commented line with:

Condition can be ignored as there is no other options.

 

else {
    g_form.setMandatory('u_in_progress_status', false);
    g_form.setMandatory('u_resolved_status', false);
    g_form.setMandatory('u_canceled_status', false);
    g_form.setDisplay('u_in_progress_status', false);
    g_form.setDisplay('u_resolved_status', false);
    g_form.setDisplay('u_canceled_status', false);
    g_form.setDisplay('u_new_status', false);
}

 

 

 

*************************************************************************************************************
"If you found my answer helpful, please give it a like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

Thank You
Juhi Poddar
****************************************************************************************************************

Runjay Patel
Giga Sage

Hi @Community Alums ,

 

Couple of thing to note.

  1. Make sure you are using exact filed name and value to compare.
  2. When ever you are hiding field first you make that filed non mandatory and then hide.
  3. You have not written any else condition that check if state is empty then make field non mandatory and hidden.

 

Use below case

 

var status = g_form.getValue('u_state');
   if(status == 'new'){
	g_form.setMandatory('u_in_progress_status', false);
	g_form.setMandatory('u_resolved_status', false);
	g_form.setMandatory('u_canceled_status', false);
	g_form.setVisible('u_new_status', true);
	g_form.setMandatory('u_new_status', true);
	g_form.setDisplay('u_in_progress_status', false);
	g_form.setDisplay('u_resolved_status', false);
	g_form.setDisplay('u_canceled_status', false);
   }else if(status == 'in_progress'){
	g_form.setMandatory('u_new_status', false);
	g_form.setMandatory('u_resolved_status', false);
	g_form.setMandatory('u_canceled_status', false);
	g_form.setVisible('u_in_progress_status', true);
	g_form.setMandatory('u_in_progress_status', true);
	g_form.setDisplay('u_new_status', false);
	g_form.setDisplay('u_resolved_status', false);
	g_form.setDisplay('u_canceled_status', false);
   }else if(status == 'resolved'){
	g_form.setMandatory('u_new_status', false);
	g_form.setMandatory('u_in_progress_status', false);
	g_form.setMandatory('u_canceled_status', false);
	g_form.setVisible('u_resolved_status', true);
	g_form.setMandatory('u_resolved_status', true);
	g_form.setDisplay('u_new_status', false);
	g_form.setDisplay('u_in_progress_status', false);
	g_form.setDisplay('u_canceled_status', false);
   }else if(status == 'Canceled'){
	g_form.setMandatory('u_new_status', false);
	g_form.setMandatory('u_in_progress_status', false);
	g_form.setMandatory('u_resolved_status', false);
	g_form.setVisible('u_canceled_status', true);
	g_form.setMandatory('u_canceled_status', true);
	g_form.setDisplay('u_new_status', false);
	g_form.setDisplay('u_in_progress_status', false);
	g_form.setDisplay('u_resolved_status', false);
   }else{
g_form.setMandatory('u_new_status', false);
 	g_form.setMandatory('u_in_progress_status', false);
 	g_form.setMandatory('u_resolved_status', false);
	g_form.setMandatory('u_canceled_status', false);
	g_form.setDisplay('u_in_progress_status', false);
	g_form.setDisplay('u_resolved_status', false); 	
g_form.setDisplay('u_canceled_status', false); 	
g_form.setDisplay('u_new_status', false);

}

 

Note: Also better you can use UI Policy for this.

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------