javascriptdynamics-crmmicrosoft-dynamicsdynamics-crm-365microsoft-dynamics-webapi

Retrieve Navigation Property Dynamics WebAPI


I am trying to retrieve the properties of a child entity via the parent entity. General Ledger Account is the parent entity and I want the details for all General Ledger Action entities that are related to the General Ledger Account. General Ledger Action has a lookup field to General Ledger Account. I am using and to build the url's and retrieve the data.

According to Microsoft Documentation:

Unless you specify a smaller page size, a maximum of 5000 entities will be returned for each request. If there are more entities that match the query filter criteria, a @odata.nextLink property will be returned with the results. Use the value of the @odata.nextLink property with a new GET request to return the next page of data.

I am only returning 1 parent entity and 1 child entity so I shouldn't be hitting this barrier. However,

When I use the url:

https://crm.com/api/data/v8.0/ccseq_generalledgeraccounts(48513938-7d5a-e711-80e5-005056b33317)/ccseq_generalledgeraccount_ccseq_generalledgeraction_GeneralLedgerAccountID

I get the results:

{
  "@odata.context":"https://crm.com/api/data/v8.0/$metadata#ccseq_generalledgeractions","value":[
    {
      "@odata.etag":"W/\"676070510\"","_organizationid_value":"ff05a89b-16b0-44a6-879c-26866b3a9d9d","ccseq_servicecode":"111","modifiedon":"2017-06-26T14:41:49Z","ccseq_generalledgernumber":"44000","ccseq_dollar":25.0000,"statecode":0,"ccseq_percent":0.0000000000,"statuscode":1,"_createdby_value":"3b0731e3-52bd-e611-80df-005056b33317","ccseq_jobclasscode":"111","ccseq_companycode":"111","_ccseq_generalledgeraccountid_value":"48513938-7d5a-e711-80e5-005056b33317","ccseq_dollar_base":25.0000,"_modifiedby_value":"3b0731e3-52bd-e611-80df-005056b33317","versionnumber":676070510,"exchangerate":1.0000000000,"ccseq_generalledgeractionid":"3e17f993-7d5a-e711-80e5-005056b33317","ccseq_geographycode":"111","createdon":"2017-06-26T14:41:49Z","_transactioncurrencyid_value":"863aa006-cae5-dc11-92e8-001a6449bbe7","_modifiedonbehalfby_value":null,"_createdonbehalfby_value":null,"utcconversiontimezonecode":null,"timezoneruleversionnumber":null,"importsequencenumber":null,"ccseq_name":null,"overriddencreatedon":null
    }
  ]
}

When I use the url:

https://crm.com/api/data/v8.0/ccseq_generalledgeraccounts?$select=ccseq_code&$expand=ccseq_generalledgeraccount_ccseq_generalledgeraction_GeneralLedgerAccountID

I get the results:

{
  "@odata.context":"https://crm.com/api/data/v8.0/$metadata#ccseq_generalledgeraccounts(ccseq_code,ccseq_generalledgeraccount_ccseq_generalledgeraction_GeneralLedgerAccountID)","value":[
    {
      "@odata.etag":"W/\"676070286\"","ccseq_code":null,"ccseq_generalledgeraccountid":"48513938-7d5a-e711-80e5-005056b33317","ccseq_generalledgeraccount_ccseq_generalledgeraction_GeneralLedgerAccountID":[

      ],"ccseq_generalledgeraccount_ccseq_generalledgeraction_GeneralLedgerAccountID@odata.nextLink":"https://crminternal.cohencpa.com/COHEN/api/data/v8.0/ccseq_generalledgeraccounts(48513938-7d5a-e711-80e5-005056b33317)/ccseq_generalledgeraccount_ccseq_generalledgeraction_GeneralLedgerAccountID"
    }
  ]
}

My understanding is that these two url's should return the exact same data which would be a json object of the parent & child attributes. What is the difference in these queries? Why is my second query returning the odata.nextLink instead of the json representation of the entity?


Solution

  • The difference is that in your first request you're retrieving data from a single record and in the second example you're retrieving all the General Ledger Accounts.

    As you can imagine at this point, the $expand works differently according if you're retrieving a single record or a collection (some time ago this was not even supported and you'd get an exception) to avoid performance issues. When retrieving collection, the WebAPI gives you the @odata.nextLink as you mentioned that you can use to retrieve the child records using a second GET.

    You can see an example of this in the MSDN:

    Retrieve related entities by expanding collection-valued navigation properties: If you expand on collection-valued navigation parameters to retrieve related entities for entity sets, an @odata.nextLink property will be returned for the related entities. You should use the value of the @odata.nextLink property with a new GET request to return the required data.