Get Optionset Label based on Optionset value using Javascript

I got one requirement to fetch optionset lable based on it’s value, then I found this post  which helped me to write required script. We can write a Soap request to fetch optionset metadata based on it’s metadata id (Guid for global optionset, you can capture global optionset guid from URL through opening global optionset record). once you have guid you can write a Soap request like below, make sure to change metadataid and optionsetname

 function GetOptionsetLable(_Value) {
   
    var _ServerURL = Xrm.Page.context.getServerUrl() + “/XRMServices/2011/Organization.svc/web”;
    var MetadaID = “xxxx-xxx-xxx…”; //Optionset guid
    var _OptionLabel = null;
    var xmlrequest = “<s:Envelope xmlns:s=”http://schemas.xmlsoap.org/soap/envelope/“>” +
    “<s:Body>” +
    “<Execute xmlns=”http://schemas.microsoft.com/xrm/2011/Contracts/Services” xmlns:i=”http://www.w3.org/2001/XMLSchema-instance“>” +
    “<request i:type=”a:RetrieveOptionSetRequest” xmlns:a=”http://schemas.microsoft.com/xrm/2011/Contracts“>” +
    “<a:Parameters xmlns:b=”http://schemas.datacontract.org/2004/07/System.Collections.Generic“>” +
    “<a:KeyValuePairOfstringanyType>” +
    “<b:key>MetadataId</b:key>” +
    “<b:value i:type=”c:guid” xmlns:c=”http://schemas.microsoft.com/2003/10/Serialization/”>”+MetadaID+”</b:value>” +
    “</a:KeyValuePairOfstringanyType>” +
    “<a:KeyValuePairOfstringanyType>” +
    “<b:key>RetrieveAsIfPublished</b:key>” +
    “<b:value i:type=”c:boolean” xmlns:c=”http://www.w3.org/2001/XMLSchema”>false</b:value>” +
    “</a:KeyValuePairOfstringanyType>” +
    “<a:KeyValuePairOfstringanyType>” +
    “<b:key>Name</b:key>” +
    “<b:value i:type=”c:string” xmlns:c=”http://www.w3.org/2001/XMLSchema”>OptionsetName</b:value>” +
    “</a:KeyValuePairOfstringanyType>” +
    “</a:Parameters>” +
    “<a:RequestId i:nil=”true” />” +
    “<a:RequestName>RetrieveOptionSet</a:RequestName>” +
    “</request></Execute></s:Body></s:Envelope>”;
    var req = new XMLHttpRequest();
    req.open(“POST”, _ServerURL, false)
    req.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);
    req.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
    req.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute“);
    req.send(xmlrequest);
    var resultXml = req.responseXML;

    var errorCount = resultXml.selectNodes(‘//error’).length;
    if (errorCount != 0) {
        var msg = resultXml.selectSingleNode(‘//description’).nodeTypedValue;
        alert(msg);
    }
    else {
       
        var oXmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
        oXmlDoc.async = false;
        oXmlDoc.loadXML(resultXml.xml);
        var results = oXmlDoc.getElementsByTagName(“ExecuteResponse/ExecuteResult/a:Results/a:KeyValuePairOfstringanyType/b:value/c:Options/c:OptionMetadata”);
        for (i = 0; i < results.length; i++) {
            var _Name = results[i].selectSingleNode(‘./c:Label/a:LocalizedLabels/a:LocalizedLabel/a:Label’).nodeTypedValue;
            var Optionvalue = results[i].selectSingleNode(‘./c:Value’).nodeTypedValue;
            if (_Value == Optionvalue) {
                _OptionLabel = _Name;
                break;
            }
        }
        return _OptionLabel;
    }
    return _OptionLabel;
}

Enjoy !!

Leave a Reply

Your email address will not be published. Required fields are marked *