Disable/ remove choice list options on the form

ushakarthya
Kilo Contributor

Hi can anyone help me out on how can we either disable/removing the choice list options on the form.

I tried couple of things which did not work fully..

Scenario: I have a cat(1,2) and subcat(1- subcat1, 2- subcat2, subcat3) where subcat values are dependent on cat values. I need one of my subcat value(subcat3) to be removed/disabled based on cat value(2). I have used removeOption and disableOption both seems to be working on load.. but when I change the values and reset again it shows the entire list.

Can you please share your valuable suggestions.

Thank You.

19 REPLIES 19

Harsh Vardhan
Giga Patron

Hi Usha,



can you share your code?



Thanks,


Harshvardhan


Hi Harsha,



I have configured a client script which runs on change of 'cat'.



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



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


  g_form.removeOption('u_sub_category', 'riskrjct');


  return;


  }


  if(newValue == 'unknowns') {


  g_form.clearOptions('u_sub_category');


  g_form.removeOption('u_sub_category', 'riskrjct');


  g_form.addOption('u_sub_category', 'riskacpt', 'Unknown Risk Acceptance');


  }


  //Type appropriate comment here, and begin script below



}


Harsh Vardhan
Giga Patron

By the way if you want this kind of functionality on form level then you don't need to write script. simply you can use OOTB "dependent field" to solve your requirement.


Check the below link.


Simple way of Dependent Dropdown values in ServiceNow - YouTube



but if you want to do on catalog form then simply write onChange() catalog client script.


Check the below script and change the field name and values of your choice list according to your values.




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


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


          return;


    }


   


  var req= g_form.getValue('<Category field name>');


// alert(req);


  if(req != 'category value1' &&   req =='category value2' ){


  g_form.addOption('<sub category field name>','<value>','<label>');



  g_form.removeOption('<sub category field name>', '<value>');


  g_form.removeOption('<sub category field name>', '<value>');



  }


  if(req == 'category value1' &&   req !='category value2' ){


  g_form.addOption('<sub category field name>','<value>','<label>');



  g_form.removeOption('<sub category field name>', '<value>');


  g_form.removeOption('<sub category field name>', '<value>');



  }


  }



Thanks,


Harshvardhan


The above code has been written on onChange() client script.