CRM 2011 - Get Optionset Text Using Javascript RRS feed

  • Question

  • Hi Forum,

    I am working on the quote entity where I have to read text value of the "shippingmethodcode" optionset.  

    where 1 = FedEx, 2= UPS and etc....

    I am using the method shown in SDK where I create webresource  for file Metadata.js ( new_metadata.js) and then

    including this webresource in my Quote form library list. I am calling this on 'Onload event' thru some custom function.

    I am using the code provided in this blog which I believe also exist in SDK and other blogs.


    but somehow I am getting follwoing error in Metadata.js file during dubugging. (Please see below screenshot.)

    Assuming code in SDK is error free so something must be wrong at my end.

    Is there any obvious thing I am missing here ??

    Thanks in advance.

    Tuesday, March 13, 2012 5:09 AM


All replies

  • Hi,

     Try the following code:

    var faultXml = resp.responseXML; var errorMessage = "Unknown (unable to parse the fault)"; if (typeof faultXml == "object") { if(faultXml.firstChild && faultXml.firstChild.firstChild) { var bodyNode = faultXml.firstChild.firstChild; //Retrieve the fault node if(bodyNode && bodyNode.childNodes) { for (var i = 0; i < bodyNode.childNodes.length; i++) { var node = bodyNode.childNodes[i]; //NOTE: This comparison do

    es not handle the case where the XML namespace changes if ("s:Fault" == node.nodeName) { for (var j = 0; j < node.childNodes.length; j++) { var faultStringNode = node.childNodes[j]; if ("faultstring" == faultStringNode.nodeName) { errorMessage = faultStringNode.text; break; } } break; } } }} }

    Tuesday, March 13, 2012 8:03 AM
  • Do you just want to get the text value of the selected option? If so you can simply use:


    Hope that helps!


    • Proposed as answer by The Hosk Tuesday, March 13, 2012 9:34 AM
    Tuesday, March 13, 2012 8:59 AM
  • Hi Surgey,

    Thanks for your suggestion but it's throwing error. I have changed the metadata file with your code but it fails on first line saying "sdk is undefined" 

    All I am trying to do is to copy fileds from Quote Entity record into the Order Entity record using Odata/Javascript.

    I have created Metada.js file as a webresource and included in my order form library list. ( Hope that's all i need to do )

    I am posting my full code. Please have a look. I have put comments where appropriate.

    Thanks again for your time.

    // Below function, I call on Order form onload.
    function GetQuoteFields() {
       // Get quote lookup id from order form so this id can be used to query quote record.
        var QuoteGuid = "";
        QuoteGuid = Xrm.Page.getAttribute("quoteid").getValue();  
        if (QuoteGuid != null) {
    function RetrieveFromQuote(Quoteid) {
        var entity = "Quote";
        var select = "?$select= ShippingMethodCode,ShipTo_Name,BillTo_Name";
        var oDataSelect;
       oDataSelect = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/" + entity + "Set(guid'" + Quoteid + "')" + select + "";
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataSelect,
            beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function(data, textStatus, XmlHttpRequest) {
               GetFields(data.d); // to process json 
            error: function(xmlHttpRequest, textStatus, errorThrown) {
                alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
    function GetFields(QuoteEntity) {
        if (QuoteEntity != null) {
            var _shippingMethod = QuoteEntity.ShippingMethodCode.Value  // this value comes back as 1,2, etc..instead of text like FedEx,UPS..
            var _billtoname = QuoteEntity.ShipTo_Name.Value;  // this value comes OK
            var _shiptoname = QuoteEntity.BillTo_Name.Value;  // this value comes OK
            if (_shippingMethod != null) 
                RetrieveOptionsetLabel(_shippingMethod); // Call to MetaData Service Passing Optionset integer value.
            Xrm.Page.getAttribute("new_billto").setValue(_billtoname); // this value comes OK
            Xrm.Page.getAttribute("new_shipto").setValue(_shiptoname); // this value comes OK
    /* ============== Get Optionset text ================================ */
    function RetrieveOptionsetLabel(OptionSetValue) {
        // Entity schema name 
        var entityLogicalName = "quote";
        // option set schema name
        var RetrieveAttributeName = "ShippingMethodCode";  // I have also tried with "shippingmethodcode"
        // Target Field schema name to which optionset text needs to be assigned
        var AssignAttributeName = "new_shippingmethod";  // this field on  order form.
        // Option set value for which label needs to be retrieved
        var stateValue = OptionSetValue;
        // Calling Metadata service to get Optionset Label
        SDK.MetaData.RetrieveEntityAsync(SDK.MetaData.EntityFilters.Attributes, entityLogicalName, null, false, function(entityMetadata) { successRetrieveEntity(entityLogicalName, entityMetadata, RetrieveAttributeName, stateValue, AssignAttributeName); }, errorDisplay);
    // Called upon successful metadata retrieval of the entity
    function successRetrieveEntity(logicalName, entityMetadata, RetrieveAttributeName, OptionValue, AssignAttributeName) {
        /// Retrieves attributes for the entity 
        var success = false;
        for (var i = 0; i < entityMetadata.Attributes.length; i++) {
            var AttributeMetadata = entityMetadata.Attributes[i];
            if (success) break;
            if (AttributeMetadata.SchemaName.toLowerCase() == RetrieveAttributeName.toLowerCase()) {
                for (var o = 0; o < AttributeMetadata.OptionSet.Options.length; o++) {
                    var option = AttributeMetadata.OptionSet.Options[o];
                    if (option.OptionMetadata.Value == OptionValue) {
                        Xrm.Page.getAttribute(AssignAttributeName).setValue(option.OptionMetadata.Label.UserLocalizedLabel.Label); // Setting optionset text value
                        success = true;
    function errorDisplay(XmlHttpRequest, textStatus, errorThrown) {

    Tuesday, March 13, 2012 10:45 PM
  • Thanks Paul for your response but I am  using odata and javascript to fetch my quote details in order to populate some fields from it on associated Order record.



    Tuesday, March 13, 2012 10:47 PM
  • HI H.Desai,

    This code look ok, but does not work). I offer you to use the RetrieveAttributeRequest for get label from OptionSet. You can fined example here:

    Performing a Javascript Web Service call to the Metadata Service

    Retrieve the Metadata for an Attribute in Microsoft Dynamics CRM 2011 Using .NET or Jscript

    Wednesday, March 14, 2012 7:54 AM
  • Hi Sergey,

    Yes, it's bit unusal as I am simply using metadata file provided and following line always gives me null value.

    var attributeData = resp.responseXML.selectSingleNode("//b:value");

    There is a value in optionset so it should pick up.

    anyway, I will try to dig it more or will use options you have provided.

    Thank you very much.

    Wednesday, March 14, 2012 11:23 PM