locked
using oData and javascript to retrieve record form CRM 2011 DB RRS feed

  • Question

  • Hi all,

     

    I've tried to use the below code sample and the Jquery web resource on the onLoad event of account to retrieve record form CRM 2011 DB:

     

    function init()
    {
     // write required ODATA query
     var guid = Xrm.Page.data.entity.getId();
     var odataSelect = "http://server/organization/XRMServices/2011/OrganizationData.svc/AccountSet(guid'D55AE4C2-E144-E011-93C2-000C297ED18C')";
    
     $.ajax({
      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)
      {
    
       RetrieveEntityData(data.d);
    
      },
      error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
     });
    }
    
    
    
    function RetrieveEntityData(Entity)
    {
     // get the fields from the Entity object
     var accountNumber = Entity.AccountNumber;
     var AccountName = Entity.Name;
    
     alert("Account Name "+Entity.Name);
    }
    

     but the problem is that this web resource use a static GUID so that whatever the loaded account it retrieve the name of a specific account, I need to update the above code to use a dynamic GUID using "Xrm.Page.data.entity.getId()"

     

    could anybody please help me on this ??

     


    Sunday, May 8, 2011 4:10 PM

Answers

  • var guid = Xrm.Page.data.entity.getId();
    var odataSelect = "http://server/organization/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" + guid + "')";
    

    --pogo (pat)
    Sunday, May 8, 2011 7:07 PM

All replies

  • var guid = Xrm.Page.data.entity.getId();
    var odataSelect = "http://server/organization/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" + guid + "')";
    

    --pogo (pat)
    Sunday, May 8, 2011 7:07 PM
  • Try this it may help you.

    function OnLoad() {

        var context = Xrm.Page.context;

        var serverUrl = context.getServerUrl();

        var guid = Xrm.Page.data.entity.getId();

        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

        // Specify the ODATA entity collection

        var ODATA_EntityCollection = "/AccountSet";

        //Asynchronous AJAX function to Create a CRM record using OData

       $.ajax({

       type: "GET",

       contentType: "application/json; charset=utf-8",

       datatype: "json",

       url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + "(guid'" + guid + "')",

       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) {

                alert("success");

                var name = data["d"];

                alert(name.Name);

            },

            error: function (XMLHttpRequest, textStatus, errorThrown) {

                alert("failure");

            }

        });

    }

    Tuesday, August 13, 2013 4:52 AM