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…
Here are a couple of practical examples…
function onChange(control, oldValue, newValue, isLoading) {
g_form.removeOption('priority', '1');
}
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…
Options can be added to a choice list by using the ‘addOption’ function call…
Here’s an example…
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:
Active: true
Global: true
Script:
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…
Here are the same examples from above modified to use the ‘disableOption’ call instead of the ‘g_form.removeOption’ call.
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.
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.
Here’s an example of the ‘enableOption’ call.
function onChange(control, oldValue, newValue, isLoading) {
enableOption('priority', '1');
}
Comments
Posted On
Mar 17, 2010Posted By
sylvain.aspediensExcellent, it works perfectly! Thanks for this article!!!
Posted On
Jun 01, 2010Posted By
David MartinRemove 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, 2011Posted By
Rick MannMark
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.
var isEwr = g_user.hasRole('ewr_manager');
var ewrStatus = g_form.getValue('u_status');
if (!isEwr && (u_status != 8)){
g_form.removeOption('u_status', '8');
}
if (!isEwr && (u_status != 9)){
g_form.removeOption('u_status', '9');
}
}
Posted On
Mar 01, 2011Posted By
Mark StangerI 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, 2011Posted By
Sandy SeegersMark,
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, 2011Posted By
Mark StangerI 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, 2011Posted By
Tony BalestreriThe 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, 2011Posted By
Mark StangerI 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, 2011Posted By
Daniel StyerHow do you add or remove an opton with a wizard multiple choice field?
Posted On
Jul 05, 2011Posted 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, 2011Posted By
sandyi 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.
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, 2011Posted By
Mark StangerIf 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, 2011Posted By
Alex AlarconMark, does the addOption() function only work with choice lists or can it work with catalog variables such as a checkbox?
Posted On
Dec 21, 2011Posted By
Mark StangerThese functions are designed to be used with standard choice lists only.