Javascript – Control OptionSet values

Here is an example of a function to limit the values of an optionset, in this example I have a field called priority and want to be able to control when some options are selectable.

First off all, I define the options and text values that are possible in the optionset, theses are global variables.

var low = {value : 100000000, text : "Low"};
var medium = {value : 100000001, text : "Medium"};
var high = {value : 100000002, text : "High"};
var reallyHigh = {value : 100000003, text : "REALLY REALLY HIGH"};

Then this function, removes all of the current values and adds back just the ones I want to be selectable. This function is then calls from anywhere needed.

As either popOptSet(true); – to show all values.

Or popOptSet(false); – to show a restricted set.

function popOptSet(all) {
  var pickList = Xrm.Page.getControl("new_priority");
  var options = Xrm.Page.getAttribute("new_priority").getOptions();

  // *** Clear current items
  for (var i = 0; i < options.length; i++) {
    pickList.removeOption(options[i].value);
  }

  // *** Now add back just what is needed
  if (all == true) {
    pickList.addOption(reallyHigh);
  }

  pickList.addOption(low);
  pickList.addOption(medium);
  pickList.addOption(high);
}

Hopefully you can amend this concept for you own purposes.

Happy coding! J

5 thoughts on “Javascript – Control OptionSet values

  1. Hi,
    it seems that your code contains a bug.

    Please pay attention to the difference between Xrm.Page.getControl(“new_priority”) and Xrm.Page.getAttribute(“new_priority”)

    “getControl” doesn’t implement getOptions method.

    Your code should be something like this:
    for (var i = 0; i < Xrm.Page.getAttribute("new_priority").getOptions().length; i++)
    {
    Xrm.Page.getControl("new_priority").removeOption(Xrm.Page.getAttribute("new_priority").getOptions()[i].value);
    }

    Giorgio – Spagetticode.eu

    Like

  2. Can you please tell me why to go for lookup fields instead of optionset? I hear from people that Lookupfields are the best solution for future updates.

    Like

    • Great question. If you list of values is short and unlikely to change optionsets are a good choice. As quick to enter for users. But if you have long lists or expect the options to often change then a lookup provides more flexibility. Another advantage of optionsets can be found if you need the multi-select feature. Both approaches are supported and fine in terms of upgrades. It is just that with lookups you can add values (or even users can add values) without needing to deploy a new version of your solution.

      Like

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s