One common configuration task in ServiceNow is to disable (make read-only) or remove certain select options from a choice list of a particular choice field. By far, the most common scenario where I’ve seen this is when a customer wants to restrict access to some ‘Closed’ type of option in a choice list depending on the role of the user. This is so common, in fact, that it has been worked into an Incident Management Best Practices plugin. In general, removing or adding choice list options is pretty easy to do, but there are a few things that you need to look out for. Disabling or enabling choice list options is not as simple just because it’s not a built-in function in ServiceNow. In this post, I’ll show you how to do both!

Removing/Adding Options

You can remove options from a choice list by using the following function call…

g_form.removeOption(<fieldName>, <choiceValue>);

Here are a couple of practical examples…

//Remove the 'Critical' priority option when some field changes
function onChange(control, oldValue, newValue, isLoading) {
   g_form.removeOption('priority', '1');
}
If you’re removing options in an ‘onLoad’ script make sure that you only remove the option if it is not the currently selected option as shown here.
//Remove the 'Closed' state option if the user is not an admin and state is not 'Closed'
function onLoad() {
   var isAdmin = g_user.hasRole('admin');
   var state = g_form.getValue('state');
   if (!isAdmin && (state != 7)){
      //alert('Current user is not an admin');
      g_form.removeOption('state', '7');
   }
}

If you want to remove ALL options from a choice list you can use the following…

g_form.clearOptions('<FIELD_NAME>');

Options can be added to a choice list by using the ‘addOption’ function call…

g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);
Note that the ‘targetIndex’ parameter is a numeric, zero-based value that allows you to specify the point in the choice list where an option should be inserted. So, if you had a choice list with 5 options and you wanted your option to be added as the third option, your target index would be the number 2. If your choice list contains a ‘–None–‘ value, you must include that in your count (usually the 0 index). ‘targetIndex’ is an optional parameter. If no target index is specified, then the option will be added to the end of the choice list.

Here’s an example…

//Add the 'Critical' priority option as the second option (--None-- value is option 1) when some field changes
function onChange(control, oldValue, newValue, isLoading) {
   g_form.addOption('priority', '1', '1 - Critical', 1);
}

Disabling/Enabling Options

There is no out-of-box function provided to disable options in choice lists.

The reason for this is that the Chrome browser didn’t support the method and removed the options instead. It is possible to disable choice list options in other browsers though, but you’ll have to provide the function to do so. In order to do that, you can create a new UI Script by navigating to ‘System UI -> UI Scripts’ and entering the following settings:

UI Script SettingsName: DisableEnableOption (Make sure to avoid any special characters in the naming of UI scripts)
Active: true
Global: true
Script:

function disableOption(fieldName, choiceValue) {
   fieldName = g_form.removeCurrentPrefix(fieldName);
   var control = g_form.getControl(fieldName);
   if (control && !control.options) {
      var name = 'sys_select.' + this.tableName + '.' + fieldName;
      control = gel(name);
   }
   if (!control)
      return;

   if (!control.options)
      return;

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

function enableOption(fieldName, choiceValue) {
   fieldName = g_form.removeCurrentPrefix(fieldName);
   var control = g_form.getControl(fieldName);
   if (control && !control.options) {
      var name = 'sys_select.' + this.tableName + '.' + fieldName;
      control = gel(name);
   }
   if (!control)
      return;

   if (!control.options)
      return;

   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;
      }
   }
}

Once you have created your global UI Script functions, you can use it in almost the exact same way as the ‘removeOption’ function above. The only difference is that there is no need to prefix the function call with ‘g_form’.

You can disable options from a choice list by using the following function call…

disableOption(<fieldName>, <choiceValue>);

Here are the same examples from above modified to use the ‘disableOption’ call instead of the ‘g_form.removeOption’ call.

//Disable the 'Critical' priority option when some field changes
function onChange(control, oldValue, newValue, isLoading) {
   disableOption('priority', '1');
}

Again, you probably will only want to disable the option if it is not the option selected when the form loads.

//Disable the 'Closed' state option if the user is not an admin and state is not 'Closed'
function onLoad() {
   var isAdmin = g_user.hasRole('admin');
   var state = g_form.getValue('state');
   if (!isAdmin && (state != 7)){
      //alert('Current user is not an admin');
      disableOption('state', '7');
   }
}

Similarly, you can enable options in a choice list by using the following function call. Since the option is not actually removed from the list, it’s not necessary to provide the choice index.

enableOption(<fieldName>, <choiceValue>);

Here’s an example of the ‘enableOption’ call.

//Enable the 'Critical' priority option when some field changes
function onChange(control, oldValue, newValue, isLoading) {
   enableOption('priority', '1');
}