CRM 2011 - Javascript access to fields from an associated (1:N) entity

Proposed Answer CRM 2011 - Javascript access to fields from an associated (1:N) entity

  • vendredi 4 mai 2012 19:49
     
     
    I have been using the Xrm.Page.getAttribute method to access fields in the current entity. I would like to drill down and access fields from associated entities. Entity associations have been made using "lookup" type fields. I have gone as far as finding the GUID of the associated entity. What's next?

Toutes les réponses

  • vendredi 4 mai 2012 21:26
     
     Réponse proposée

    You can pull the data from another entity using the OData service.

    Here is an example of pulling the ParentCustomerID of a record based on the customer id.

    var idCustomer = Xrm.Page.getAttribute("customerid").getValue()[0].id;
     
    var retrieveRecordsReq = new XMLHttpRequest();
      var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
        
      retrieveRecordsReq.open('GET', ODataPath+"/ContactSet(guid'" + idCustomer + "')",false);
      retrieveRecordsReq.setRequestHeader("Accept", "application/json");
      retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
      retrieveRecordsReq.send();

      var records = this.parent.JSON.parse(retrieveRecordsReq.responseText).d;
       alert(records.ParentCustomerId.Id);

    • Proposé comme réponse CRM Nerd vendredi 4 mai 2012 21:28
    •