Removing or Disabling Choice List Options

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…

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…

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 changesfunction 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:

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.

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.

Share This Story, Choose Your Platform!

Mark has worked in the IT industry since 2002 and with ServiceNow since 2007. He is the founder and creator of SN | Guru and the co-founder of Crossfuze, one of the worlds leading ServiceNow consulting partners. Prior to co-founding Crossfuze, he worked for ServiceNow as a Senior Architect on the Professional Services team.
He has personally led dozens of successful implementations encompassing every part of the ServiceNow platform. He is also responsible for designing and developing groundbreaking ServiceNow solutions and best practices in the form of various applications, turnkey solutions, and integrations during his tenure at ServiceNow, Crossfuze and, of course, SN | Guru. These solutions are used today by ServiceNow administrators and consultants alike in hundreds of ServiceNow instances around the world!

36 Comments

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.

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.

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?

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.

@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.

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.

I used this script. It works, but my problem is how can I make it reset to the original Value?

I am working on the Change Request. If the Type = Standard it will only show the State = 5. It works, when I change to other type, but when I go back to Type = Standard it still disabled the choicelist option.

Basically how would I do a OnChange for a field where 2 conditions need to be met?, saying that fields 1 and 2 both need to change to a certain value for field 3 to disable certain options – is that even possible with a OnChange?

You can’t hide them from a list because you can’t run the necessary client scripts there to do so. If you’re hiding choice options, then you also need to restrict list editing for that field with a ‘list_edit’ ACL.

And it is working fine. But once the form is submitted, the value is getting removed. It no longer shows that the value is oresent in the field. When i tried to add the choice to the variable field, the choice was visible twice.

The removal and addition code is working correctly, but you’re not handling all of the various scenarios associated with it. Your onChange script is also running onLoad, so that’s probably the cause of some of the confusion. You should also only remove an option if it isn’t currently selected.

The solutions above we use today to remove options from standard choice lists in forms.I have had a request to change some options in an existing Select Box in a Variable Set. Of course any changes to choices in a select box effect existing RITMs that were ordering using the choice I’m trying to remove. I’ve looked up and down and can’t find a good solution on removing/hiding those…can anyone point me to a solution?

I had a problem with the disableOption working correctly. I noticed that you don’t specify “g_form.” like you do for the removeOption. Once I added the “g_form.” like this: g_form.disableOption() it worked.

If I read this correctly in the SNow wiki release notes sections, both g_form.enableOption and g_form.disableOption were enabled in Fall 2010 Stable 3, but they were both removed as of the June 2011 Preview 1. If this is correct Mark, your functions are still the only way to perform these tasks.

Is there any way to hide an option from a reference field. For example I have a field that looks up groups but in certain scenarios I do not want the user to be able to select some of them. Is there any way that you know of to accomplish this using removeOption or something similar?

Do you know why companies like Amazon, Zappos, Southwest Airlines, or the plumber in the next town keep getting more business from consumers like us? The answer is quite simple. It’s because they provide exceptional customer service every chance possible. Their entire brand is built on the promise of great service. Sounds easy, right? On […]

We are excited to announce the official release of our new book, 10 Pillars of ServiceNow Success for CIOs. The book is an educational resource to guide CIOs and their teams on their journey to transforming their organizations using ServiceNow. The 75-page book discusses best practices derived from more than 500 ServiceNow implementations—all aimed at […]

Famous leaders on the world stage have struggled to articulate a vision to gain the kind of mass support that changes history. Without a clear and consistently communicated vision, momentum for the cause...

Welcome to Part X of Crossfuze’s 10 Pillars of ServiceNow Success for CIOs blog series! First, a story to illustrate the challenge of fully integrating your ServiceNow ecosystem: Kyle is an experienced CIO...

Welcome to Part IX of Crossfuze’s 10 Pillars of ServiceNow Success for CIOs blog series! First, a story about the universal, pervasive challenge of demand management: Jason is a veteran CIO who has made many friends and allies within his company by being a “yes man.” He bends over backward to fulfill everyone’s IT service […]

Since 2009, ServiceNow Guru has been THE go-to source of ServiceNow technical content and knowledge for all ServiceNow professionals. Whether you're a new admin or a seasoned consultant, you're guaranteed to find quality solutions that will aid you in your ServiceNow journey!