How to hide choices from choice list using client script onchange

swathigangadhar
Tera Expert

How to hide choices from choice list using client script onchange, i want to hide few choices in subcategories when category is changed, i tried using g_form.removeOption('subcategory', '7'); but no luck, how to solve this?

16 REPLIES 16

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Swathi,



Just check the value and fieldname while using removeOption method and it should work



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

shloke04
Kilo Patron

Hi,



You can achieve this by making the Subcategory field dependent on the Category field as mentioned below:



1) Right click on the Subcategory field and click on configure Dictionary and then scroll down to the Dependent Tab and select the Dependent field as "Category" as shown below:



find_real_file.png





Once the   dependent field has been setup, then While defining the choices there is a field called "Dependent Value" where you can define the parent choice value on which the child value should be dependent on as shown below:



find_real_file.png



Like for e.g. in the above screen shot Dependent Value is Hardware present as a choice value in the category field.



Or



You can do this with the help of a client script also as mentioned below:



Write an on change Client Script on the Category field and write the below script:



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


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


  return;


  }


  var cat = g_form.getValue('Category');


  if(newValue == 'New Category Value')           // Replace the New Category Value with the value you want to have the validation


  {


  g_form.clearOptions('Field Name');


  g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');


  g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');


    }



else if(newValue == 'New Category Value')


{


g_form.clearOptions('Field Name');


  g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');


  g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');


      }




Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

vspk
Giga Expert

Hi Swathi,



Try writing an UI policy.


In the 'When to Run' condition, give the required category name.


Goto Advanced View.


in the script, apply g_form.remove condition.



Regards,


Venkata Sneha


Harish KM
Kilo Patron
Kilo Patron

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


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


          return;


    }


var val = g_form.getValue('fieldname');


  alert(val);


  if(val=='value you want')


  {


  alert('inside');


  g_form.removeOption('fieldname', 'value');


  }


Regards
Harish