locked
CRM 2013 Odata JavaScirpt how get optionset value.? RRS feed

  • Question

  • I am using CRM 2013...

    I want to get optionset value retrun from Odata Rest Single Retrieve query

    I am using this code i know to get textfiled value but how get Optionset value return by jquery Odata

     function Template_onchange() {
        var eventTemplateId = Xrm.Page.getAttribute("new_fromtemplate").getValue() != null ? Xrm.Page.getAttribute("new_fromtemplate").getValue()[0].id : null;
        if (eventTemplateId != null) {
            var account = GetEventTemplateData(eventTemplateId);
            if (account != null && account[0] != null) {
                      alert(account[0].new_MaxNumberofParticipants);
                
            }
        }
    }
    
    
    function GetEventTemplateData(eventTemplateId) {
        var serverUrl = Xrm.Page.context.getServerUrl();
        var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/new_eventtempalteSet(guid'" + eventTemplateId + "')?$select=*";
        var jSonArray = new Array();
    
        jQuery.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataUri,
            async: false,
            beforeSend: function (XMLHttpRequest) {
                //Specifying this header ensures that the results will be returned as JSON.            
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                if (data && data.d != null) {
                    jSonArray.push(data.d);
                }
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("Error :  has occured during retrieval of the Account details");
            }
        });
    
        return jSonArray;
    }


    Muhammad Sohail


    Thursday, January 9, 2014 3:00 PM

Answers

  • Hi Sohail,

    The easiest way to do it is using IE developer, place break point and watch your data.d return. You can check all the attributes there.

    Usually option value will be return with this format: entity.new_optionsetfield.value (if entity.new_optionsetfield != null).

    Hope it helps.


    I getting error using this as you said LinhGiang "entity.new_optionsetfield.value"
    but i used Capital V of value as 

    alert(account[0].new_ContributionType.Value);

    like this it is works thanks

    Muhammad Sohail

    • Marked as answer by sohail450 Friday, February 7, 2014 7:26 AM
    Friday, February 7, 2014 7:26 AM

All replies

  • Hi Sohail,

    The easiest way to do it is using IE developer, place break point and watch your data.d return. You can check all the attributes there.

    Usually option value will be return with this format: entity.new_optionsetfield.value (if entity.new_optionsetfield != null).

    Hope it helps.

    Friday, January 10, 2014 2:53 AM
  • Hi,

    REST/OData you are only able to query for optionset value (int), not the label (text). If it is the label you are after than you would need to use SOAP to get that because metadata (e.g. label) is only available by executing messages, something that REST/OData cannot do. 

    Look for Sample: Retrieve entity metadata using JavaScript inside CRM 2013 SDK. Here is the CRM 2011 reference on this topic which is still relevant.


    Ronald

    Friday, January 10, 2014 3:26 AM
  • Thanks for reply I used the

    alert(account[0].new_ContributionType.Value);

    it is working but what about Currency filed I am using same procedure but not setting the currenecy value. it give the below error

    "This control only accept numbers or null as input."


    Muhammad Sohail

    Monday, January 13, 2014 7:35 AM
  • Hi,

    As you said:

    The point get option set value (int) from REST/OData is working now.

    You now have a "setting currency value problem"? Can you be more specific? why do you use the same procedure (mechanism?) for option set and currency value?

    To set value to a field using JScript, you can always check SDK for example.

    Hope it helps.


    • Edited by Linh Giang Monday, January 13, 2014 10:20 AM
    Monday, January 13, 2014 10:19 AM
  • Saturday, January 18, 2014 5:57 AM
  • Hi Sohail,

    The easiest way to do it is using IE developer, place break point and watch your data.d return. You can check all the attributes there.

    Usually option value will be return with this format: entity.new_optionsetfield.value (if entity.new_optionsetfield != null).

    Hope it helps.


    I getting error using this as you said LinhGiang "entity.new_optionsetfield.value"
    but i used Capital V of value as 

    alert(account[0].new_ContributionType.Value);

    like this it is works thanks

    Muhammad Sohail

    • Marked as answer by sohail450 Friday, February 7, 2014 7:26 AM
    Friday, February 7, 2014 7:26 AM