Xrm.Page object provides several methods to work with OptionSet attributes, the most used are:
The library uses only supported JavaScript and doesn't query the metadata to retrieve the labels. It contains the following functions:
- Xrm.Page.getAttribute("optionset").getValue() - returns the selected item value
- Xrm.Page.getAttribute("optionset").getText() - returns the selected item label
The library uses only supported JavaScript and doesn't query the metadata to retrieve the labels. It contains the following functions:
- opt.GetValues(fieldName)
returns an array with all the values of a specific OptionSetvar freightTermsValues = opt.getValues("address1_freighttermscode"); alert("Values inside Freight Terms OptionSet: " + freightTermsValues);
- opt.GetLabels(fieldName)
returns an array with all the labels of a specific OptionSetvar shippingMethods = opt.getLabels("address1_shippingmethodcode"); alert("Available Shipping Methods: " + shippingMethods);
- opt.GetLabel(fieldName, value)
returns the corresponding label for a specific OptionSet valuevar paymentTermLabel = opt.getLabel("paymenttermscode", 2); alert("Payment Term with Value 2 is: " + paymentTermLabel);
- opt.GetValue(fieldName, label)
returns the corresponding value for a specific OptionSet labelvar primaryValue = opt.getValue("address1_addresstypecode", "Primary"); alert("Value for address type Primary is: " + primaryValue);
if (typeof (opt) == "undefined") { opt = {}; } opt.getValues = function (fieldName) { var values = []; var attribute = Xrm.Page.getAttribute(fieldName); if (attribute != null && attribute.getAttributeType() == "optionset") { var options = attribute.getOptions(); for (var i in options) { if (options[i].value != "null") values.push(options[i].value * 1); } } return values; }; opt.getLabels = function (fieldName) { var labels = []; var attribute = Xrm.Page.getAttribute(fieldName); if (attribute != null && attribute.getAttributeType() == "optionset") { var options = attribute.getOptions(); for (var i in options) { if (options[i].value != "null") labels.push(options[i].text); } } return labels; }; opt.getLabel = function (fieldName, value) { var label = ""; var attribute = Xrm.Page.getAttribute(fieldName); if (attribute != null && attribute.getAttributeType() == "optionset") { var option = attribute.getOption(value); if (option != null) label = option.text; } return label; }; opt.getValue = function (fieldName, label) { if (label == "") return null; var value = null; var attribute = Xrm.Page.getAttribute(fieldName); if (attribute != null && attribute.getAttributeType() == "optionset") { var options = attribute.getOptions(); for (var i in options) { if (options[i].text == label) return options[i].value * 1; } } return value; };Note: developed with Xrm JavaScript Dojo