THURSDAY, FEBRUARY 09, 2012

Removing or Disabling Choice List Options

One common configuration task in Service-now 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 Service-now. 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');
}

Comments

Posted On
Mar 17, 2010
Posted By
sylvain.aspediens

Excellent, it works perfectly! Thanks for this article!!!

Posted On
Jun 01, 2010
Posted By
David Martin

Remove option is great, thanks.

I have a question if I may, I want to do exactly the same hide a Status but only if a Change as been created/related for a Problem. Is this possible? What could I use to check whether a change has been related.

Thanks

Dave

Posted On
Mar 01, 2011
Posted By
Rick Mann

Mark

Have you seen any issues where this type of client script would cause a form not to load correctly with Internet Explorer? I created an onLoad client script with the code below. The script does work with Chrome and Firefox.

function onLoad() {
 
   var isEwr = g_user.hasRole('ewr_manager');
 
   var ewrStatus = g_form.getValue('u_status');
 

 
   if (!isEwr &amp;&amp; (u_status != 8)){
 
      g_form.removeOption('u_status', '8');
 
   }  
 
   if (!isEwr &amp;&amp; (u_status != 9)){
 
      g_form.removeOption('u_status', '9');
 
   }
 
}
Posted On
Mar 01, 2011
Posted By
Mark Stanger

I haven’t, but I do see some errors in your ‘if’ conditions in this script. I think ‘u_status’ should be changed to ‘ewrStatus’.

Posted On
May 13, 2011
Posted By
Sandy Seegers

Mark,

We use this technique for limiting choice list selection values in several circumstances. However, we’ve run into one big problem with this approach. If we’re working in a situation with extremely slow response times, it is sometimes possible to select a ‘forbidden’ choice value before the Client Script has a chance to load. In fact, I can simulate this if I’m really quick to click on the cancel load button on my browser.

Right now I’m looking at having to add a Business Rule to enforce the selection list restrictions that I’m trying to accomplish with my Client Scripts. Looking into your crystal ball, do you foresee a possibility that choice list values could be controlled with Access Rules?

Thanx in advance

Sandy

Posted On
May 13, 2011
Posted By
Mark Stanger

I can’t think of a good way to do that with ACLs. The ‘before’ business rule approach with a ‘setAbortAction’ check is what I would use if I were having a similar problem. You might also consider an ‘onChange’ client script to validate the choice. The first place you should look though is at the client scripts and UI policies running on that form and see what you can eliminate or optimize. Even with high latency, I wouldn’t think that you would be having this issue unless you had a ton of other client scripts and policies running.

Posted On
Jun 14, 2011
Posted By
Tony Balestreri

The disableOption works perfectly for firefox and chrome however in internet explorer it does not for me. This happen to anyone else?

Posted On
Jun 14, 2011
Posted By
Mark Stanger

I just tested it successfully with IE8 at demo.service-now.com. I’ve never had any reports of issues with IE and these scripts.

Posted On
Jul 05, 2011
Posted By
Daniel Styer

How do you add or remove an opton with a wizard multiple choice field?

Posted On
Jul 05, 2011
Posted By
Mark Stanger

@Daniel, This article was intended to address choice fields, not multiple choice variables. I’m not aware of any way to do that directly, though it might be possible. You may try asking on the forums but my advice would be to use a regular choice field if you need to do this.

Posted On
Aug 04, 2011
Posted By
sandy

i have this code to remove value from hardware_substatus. it works fine when the form is loading but it does not remove “active” value when we change the field on the form. am i missing anything here.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if(g_form.getValue('sys_class_name') != 'u_cmdb_ci_handheld'){
var ch = new GlideRecord('sys_choice');
ch.addQuery('name','u_cmdb_ci_handheld');
ch.addQuery('element','hardware_substatus');
ch.query();
while(ch.next()){
g_form.removeOption('hardware_substatus', 'active');
}
Posted On
Aug 04, 2011
Posted By
Mark Stanger

If there is an issue, it wouldn’t be with ‘removeOption’ since that works equally well onLoad and onChange. From the script that you pasted I can tell you that you’re missing a couple of closing brackets but I’m not sure if you pasted the entire thing. You’ll just have to troubleshoot the issue to find out. I would start by placing a couple of alerts in that client script to see if the code gets into the ‘if’ statement and to see if it gets to the ‘while’ statement. This would help you to determine where the issue with your script is.
If the script gets all the way into your ‘while’ statement and still doesn’t work then you’ve probably got some other conflicting script in your system.

Posted On
Dec 21, 2011
Posted By
Alex Alarcon

Mark, does the addOption() function only work with choice lists or can it work with catalog variables such as a checkbox?

Posted On
Dec 21, 2011
Posted By
Mark Stanger

These functions are designed to be used with standard choice lists only.

Leave a Reply


Notify me of followup comments via e-mail. You can also subscribe without commenting.

Latest Comments

  • Aric: Finally figured out what I was doing wrong, incase someone else wants to do this. Mark is correct about needing...
  • Mark Stanger: You’ll probably need at least 3 total ACLs then. You’ll need one...
  • Mark Stanger: Hey Paul, I assume you’re talking about ‘task_ci’, not ‘task_group. If so, the...
  • ND: Hi Mark, This is cool. How can I do same at the form level. I have created a slushbucket variable and I want to...