JavaScript to get/set Option-set text and values.

Spread the Knowledge...

There are many scenarios where we need to get the option name and value of the option-set field. Below code can be used to retrieve the option name and its values.

1. To get option name from OptionSet

Below code can be used to get the option name by passing the option value.

function getOptionDetails() {
      var optionDetails = Xrm.Page.getAttribute("SchemaNameofOptionsetField").getOption(OptionsetValue); 
      if (optionDetails != null) {
           //To get text of the option set.
           var optionText = optionDetails.text;
           alert("OptionSet Text:" + optionText);
      }
}

2. To get the text and value of the selected CRM OptionSet

Below code can be used to get the text and value of selected optionset:

function getSelectedOptionset() {
     var selectedOptionset = Xrm.Page.getAttribute("SchemaNameofOptionsetField").getSelectedOption(); 
     if (selectedOptionset != null) {
          //To get text of the option set.
          var optionText = selectedOptionset.text;
          alert("OptionSet Text:" + optionText);
 
          //To get value of the option set.
          var optionValue = selectedOptionset.value;
          alert("OptionSet Value: " + optionValue);
     }
}

3. To get the value of selected CRM OptionSet.

Below code can be used to get the value of selected optionset.

function getSelectedOptionsetValue() {
     var selectedOptionsetValue = Xrm.Page.getAttribute("SchemaNameofOptionsetField").getValue(); 
     alert("Value of selected Optionset:" + selectedOptionsetValue);
}

4. To get the text of selected CRM OptionSet.

Below code can be used to get the text of selected optionset.

function getSelectedOptionsetText() {
      var selectedOptionsetText = Xrm.Page.getAttribute("SchemaNameofOptionsetField").getText();
      alert("Text of selected Optionset:" + selectedOptionsetText);
}

5. To set the option in CRM OptionSet through specific option value.

This code can be used to set the option(Text and value) in Optionset field by passing the value of the option to setValue.

function setOptionsetUsingValue() {
      Xrm.Page.getAttribute("SchemaNameofOptionsetField").setValue(NumericOptionSetValue);
}
Example: Suppose "new_accountcategory" is an optionset with below values and i got a requirement to set it as 'Saving'.
Name Value
----- ------
Saving 10000001
Current 10000002
 function setOptionsetUsingValue() {
      Xrm.Page.getAttribute("new_accountcategory").setValue(10000001);
}

6. To set the option in CRM OptionSet through Text.

This code can be used to set the option(Text and value) in Optionset field by passing the text of the option.
Actually there is no direct way to set optionset field by passing the text of the option.
In such cases we need to search all options until we got the required optionset text then get its value and use setValue method to set that optionset.

function setOptionSetByText() {
     var optionText = "Saving";
     var optionSetValues = Xrm.Page.getAttribute("SchemaNameofOptionsetField").getOptions();
     for (i = 0; i < optionSetValues.length; i++) {
          if (optionSetValues[i].text == optionText) {
          Xrm.Page.getAttribute("SchemaNameofOptionsetField").setValue(optionSetValues[i].value);
      }
   }
}

Hope this helps!!! Happy CRMing…


Spread the Knowledge...