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.

Not able to remove choice list option from choice field.

Jonnie_
Tera Contributor

Hi

i have written a onLoad client script and on based of condition i am removing the choices from choice list field using removeOption() method. 
g_form.removeOption(‘state’, 0);

g_form.removeOption(‘state’,2);

etc. 

 

Except one, all choices(which i mentioned in client script) are gettinh removed from field on form.

but the first g_form.removeOption(‘state’,0); is still visible. I don’t know why it’s happening. I checked other UI policy and client script.

6 REPLIES 6

Then use value as string.

g_form.removeOption(‘state’, '0');

g_form.removeOption(‘state’,'2');

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks,
Pratik Malviya

Ratnakar7
Mega Sage

Hi @Jonnie_ ,

 

Can you please try below 2 solutions:

 

1. First clear all options with 'g_form.clearOptions('state');' and add required options with 'g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);'.

 

2. Remove/Disable the options with looping.

 

var choiceValue='0'; 
var control = g_form.getControl('state');
var options = control.options;
   for (var i = 0; i < options.length; i++) {
      var option = options[i];
      if (option.value == choiceValue) {
         control.options[i].disabled = '';
         break;
      }
   }

 

 

I hope this solves your issue.
Mark this as Helpful / Accept the Solution if this clears your issue.

 

Regards,

Ratnakar