Working with option sets

Starting with option sets, things get a little bit more interesting. Now we are talking about a set of values stored in a structure similar to a dictionary's collection of name/value pairs.

Getting ready

We will be using the previously created solution. Open the solution if not already opened. Alternatively, you can create a brand new solution and add the Contact entity to your solution.

How to do it...

In order to work with an option set, let's follow these steps:

  1. Open the Contact entity added to the solution package.
  2. Open the main form.
  3. Add a new field defined as option set. We will define this field as not using an existing option set, and no default value.
  4. Let's add the following values:
    • Example A, Value 100,000,000
    • Example B, Value 100,000,001
    • Example C, Value 100,000,002
  5. Add a new JScript resource named JSOptionSet (new_JSOptionSet)
  6. Add the following function that reads the selected value of the option set and displays it in an alert window:
    function GetOSValue()
    {
        var sval = Xrm.Page.getAttribute("new_optionset").getSelectedOption().text;
        alert("Selected value: " + sval);
    }
  7. Back on the form, associate the function with the OnChange event of the newly created option set field.
  8. Save and publish.
  9. Open a contact and change the option set value. The following alert should be seen:
How to do it...

How it works...

Our function selects the option set element on the form, retrieves the selected value, and pulls the label property of the selected option. Alternatively the value can be retrieved if necessary by using the following line of code:

    var sval = Xrm.Page.getAttribute("new_optionset").getSelectedOption().value;

There's more...

Reading the selected value of an option set and acting on it is only the first part. How do you set a value of an option set programmatically as a result of another element on the form changing?

Assigning a value programmatically

When we need to assign a value to an option set programmatically, we need to use the Value defined, not the Label. So what do we do, start memorizing and hardcoding these numbers?

One alternative is to build a small helper function that allows us to loop through the values of the option set, and identify the value based on the label we are providing. Such a function could look as follows:

function SetOSValue(osName, osLabel)
{
     var options = Xrm.Page.getAttribute(osName).getOptions();
     for(i = 0; i < options.length; i++)
     {
        if (options[i].text == osLabel)
        Xrm.Page.getAttribute(osName).setValue(options[i].value);
     }
}

Once we find a match on the label we can assign the value retrieved directly from the system. This will also handle cases where certain values could be updated accidentally.

We can use this helper function in the context of our recipe by creating a new function, and associating it to the OnLoad event of the form. All we do here is call the helper function, passing as parameters the name of the option set, and the value we want to set as default.

function SetMe()
{
    SetOSValue("new_optionset","Example C");
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.42.149