Restrict values of a choice field based on its current status

srirao
Giga Expert

Hello Experts,

I am trying to restrict the values of a choice field based on its current value. I understand we can use   "Dependent Field" to restrict the values of based on the values of the other field but what I am trying to do here is little different.

I have a field named "Phase" and it has values viz. Concept, Define, Design, Build, Test, Release, Close.

The default value has been set to Concept. When Phase is set to Concept, it should only show Concept, Define and Close and when it is set to Define it should only show Concept, Define, Build and Close.

I am sure this must have been achieved already by your experts.

Appreciate your help.

Thanks

20 REPLIES 20

Thanks harshvardhansingh.   I think this what Michael was also proposing. I will try and update this thread.


harshvardhansingh I tried the following but no luck. Please note that the field I am actually using is the Out of the box "State" field which I renamed as Phase.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    var phaseValue = g_form.getValue('state');


  if (phaseValue == 12)


  {



  g_form.removeOption('state', '14');


  g_form.removeOption('state', '15');


  g_form.removeOption('state', '16');


  g_form.removeOption('state', '17');


  g_form.removeOption('state', '18');


  g_form.removeOption('state', '19');


  }


  else if (phaseValue == 13)


  {


  g_form.removeOption('state', '15');


  g_form.removeOption('state', '16');


  g_form.removeOption('state', '17');


  g_form.removeOption('state', '18');


  g_form.removeOption('state', '19');


    }


  else if (phaseValue == 14)


  {


  g_form.removeOption('state', '16');


  g_form.removeOption('state', '17');


  g_form.removeOption('state', '18');


  g_form.removeOption('state', '19');


    }


  else if (phaseValue == 15)


  {


  g_form.removeOption('state', '17');


  g_form.removeOption('state', '18');


  g_form.removeOption('state', '19');


  }


  else if (phaseValue == 16)


  {


  g_form.removeOption('state', '18');


  g_form.removeOption('state', '19');


    }


  else if (phaseValue == 17)


  {


  g_form.removeOption('state', '19');


  }


}


Hi Srirao,



try to use addOption() and removeOption() together.


to show the value use addOption and to hide the value use removeOption.



Let me know if it does not hellp.



Regards,


Harshvardhan


No luck Harsh harshvardhansingh


Below is what I wrote. Also, as the field "State" is of type Integer I think we need to add the integer value associated with the values of Concept, Define etc.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


if (isLoading || newValue === '') {


          return;


    }


  var phaseValue = g_form.getValue('state');


  if (phaseValue == 12)


  {


  g_form.addOption('state','Concept','Concept');


  g_form.addOption('state','Define','Define');


  g_form.addOption('state','Closed','Closed');


  g_form.removeOption('state', '14');


  g_form.removeOption('state', '15');


  g_form.removeOption('state', '16');


  g_form.removeOption('state', '17');


  g_form.removeOption('state', '18');


  g_form.removeOption('state', '19');



  }


  else if (phaseValue == 13)


  {


  g_form.addOption('state','Concept','Concept');


  g_form.addOption('state','Define','Define');


  g_form.addOption('state','Unassigned','Unassigned');


  g_form.removeOption('state', '15');


  g_form.removeOption('state', '16');


  g_form.removeOption('state', '17');


  g_form.removeOption('state', '18');


  g_form.removeOption('state', '19');


  if (g_form.getValue('u_status') == "Waiting For Approval")


  {


  g_form.removeOption('state', '20');


  }


  }


  else if (phaseValue == 14)


  {


  g_form.addOption('state','Concept','Concept');


  g_form.addOption('state','Define','Define');


  g_form.addOption('state','Unassigned','Unassigned');


  g_form.addOption('state','Design','Design');


  g_form.removeOption('state', '16');


  g_form.removeOption('state', '17');


  g_form.removeOption('state', '18');


  g_form.removeOption('state', '19');


  }


  else if (phaseValue == 15)


  {


  g_form.addOption('state','Concept','Concept');


  g_form.addOption('state','Define','Define');


  g_form.addOption('state','Unassigned','Unassigned');


  g_form.addOption('state','Design','Design');


  g_form.addOption('state','Build','Build');


  g_form.removeOption('state', '17');


  g_form.removeOption('state', '18');


  g_form.removeOption('state', '19');


  }


  else if (phaseValue == 16)


  {


  g_form.addOption('state','Concept','Concept');


  g_form.addOption('state','Define','Define');


  g_form.addOption('state','Unassigned','Unassigned');


  g_form.addOption('state','Design','Design');


  g_form.addOption('state','Build','Build');


  g_form.addOption('state','Test','Test');


  g_form.removeOption('state', '18');


  g_form.removeOption('state', '19');


  if (g_form.getValue('u_status') == "Waiting For Approval")


  {


  g_form.removeOption('state', '20');


  }


  }


  else if (phaseValue == 17)


  {


  g_form.addOption('state','Concept','Concept');


  g_form.addOption('state','Define','Define');


  g_form.addOption('state','Unassigned','Unassigned');


  g_form.addOption('state','Design','Design');


  g_form.addOption('state','Build','Build');


  g_form.addOption('state','Test','Test');


  g_form.addOption('state','Approved','Approved');


  g_form.removeOption('state', '19');


  }


}


Hi Srirao,



Please refer the below link and check the 12.1 and 12.3 section.


GlideForm (g form) - ServiceNow Wiki



Yes you need to use value on your addOption().



Regards,


Harshvardhan