Bite Sized Workaround: Removing Options from Catalog Variables

The Issue

I discovered the other day that “g_form.removeOption()” does not work on variables on a Catalog Task or Requested Item form. It works great on fields, and nearly every other g_form function works great on variables, but removeOption, clearOption, and a few others simply refuse to work.

If someone from ServiceNow is reading this, please consider this a vote to spend an entire “named” release simply fixing stuff like this rather than pushing new functionality.

The “Fix”

Simply put, I wrote a function to handle this. Feel free to copy and paste and use!
Credit to Keith Mills for providing the seeds of this code here.

variableRemoveOption("variable_name", "choice_list_value");

//This is a workaround to avoid the issue of ServiceNow's bug
//in getControl when handling variables on Catalog Tasks and Requested Items
function variableRemoveOption(field, value) {
     var handler = g_form.getPrefixHandler('v_dropdown');
     var map = handler.handlerObject.nameMap;
     var id;

     for (i = 0; i < map.length; i++){
           var name = map[i].prettyName;

           if (name == field) {
               id = map[i].realName;
               break;
           }
     }
     if(id) {
         var elem = g_form.getElement(id);
         var opts = elem;
         for(var j=0; j<opts.length; j++) {
             if(opts[j].value == value) {
                elem.remove(j);
                break;
             }
         }
     }
}


Categories: Developers, Tips and Tricks

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: