June 28, 2013

OptionSet JavaScript Helper Library

Xrm.Page object provides several methods to work with OptionSet attributes, the most used are:
  • Xrm.Page.getAttribute("optionset").getValue() - returns the selected item value
  • Xrm.Page.getAttribute("optionset").getText() - returns the selected item label
Sometimes is necessary to access all the items of an Optionset and not only the selected one, for this purpose I wrote a JavaScript library.

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 OptionSet
    var 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 OptionSet
    var shippingMethods = opt.getLabels("address1_shippingmethodcode");
    alert("Available Shipping Methods: " + shippingMethods);
    
  • opt.GetLabel(fieldName, value)
    returns the corresponding label for a specific OptionSet value
    var 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 label
    var primaryValue = opt.getValue("address1_addresstypecode", "Primary");
    alert("Value for address type Primary is: " + primaryValue);
    
Library code:
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

0 comments:

Post a Comment