I'm using the CE OData API to extract the customer accounts (and the linked parties) from a contact id. I tried using $expand twice (nested) so I could get the results in a single request :
GET /api/data/v9.2/contacts(488461d2-d22d-ed11-9db1-000d3a2ccce0)?$select=contactid,fullname&$expand=msdyn_contact_msdyn_contactforparty_contactid($select=_msdyn_associatedaccountvendorid_value,_msdyn_associatedaccountid_value,msdyn_contactforpartyid;$expand=msdyn_associatedaccountid($select=accountid,_owningbusinessunit_value,accountnumber)) HTTP/1.1
Details :
- GET api/data/v9.2/contacts(488461d2-d22d-ed11-9db1-000d3a2ccce0)
- $select : contactid,fullname
- $expand : msdyn_contact_msdyn_contactforparty_contactid
- $select : _msdyn_associatedaccountvendorid_value,_msdyn_associatedaccountid_value,msdyn_contactforpartyid
- $expand msdyn_associatedaccountid
- $select : accountid,_owningbusinessunit_value,accountnumber
On the paper, it works but some of the "account" fields are null : accountnumber and _owningbusinessunit_value for example. Response JSON :
{ "@odata.context": "foo.crm4.dynamics.com/.../$metadata#...", "@odata.etag": "W/\"52413619\"", "contactid": "488461d2-d22d-ed11-9db1-000d3a2ccce0", "fullname": "John DOE", "msdyn_contact_msdyn_contactforparty_contactid": [ { "@odata.etag": "W/\"52413817\"", "_msdyn_associatedaccountvendorid_value": null, "_msdyn_associatedaccountid_value": "8ec6dbf0-cefd-ec11-82e5-6045bd8accda", "msdyn_contactforpartyid": "1100ed03-d08c-ed11-81ad-6045bd8f91dd", "msdyn_associatedaccountid": { "accountid": "8ec6dbf0-cefd-ec11-82e5-6045bd8accda", "_owningbusinessunit_value": null, "accountnumber": null, "__DisplayName__": "Customer n°1", "IsReferencedQueryCall": true } }, { "@odata.etag": "W/\"52431925\"", "_msdyn_associatedaccountvendorid_value": null, "_msdyn_associatedaccountid_value": "aae38daf-205c-ed11-9562-6045bd8f91dd", "msdyn_contactforpartyid": "bd15d068-088d-ed11-81ad-6045bd8f91dd", "msdyn_associatedaccountid": { "accountid": "aae38daf-205c-ed11-9562-6045bd8f91dd", "_owningbusinessunit_value": null, "accountnumber": null, "__DisplayName__": "Customer n°2", "IsReferencedQueryCall": true } } ] }
If I split it in 2 requests : one to get the parties of the contact, and one to get the accounts from the party, then the values are not null.
GET /api/data/v9.2/msdyn_contactforparties(1100ed03-d08c-ed11-81ad-6045bd8f91dd)?$select=_msdyn_associatedaccountvendorid_value,_msdyn_associatedaccountid_value,msdyn_contactforpartyid&$expand=msdyn_associatedaccountid($select=accountid,_owningbusinessunit_value,accountnumber) HTTP/1.1
Details :
- GET /api/data/v9.2/msdyn_contactforparties(1100ed03-d08c-ed11-81ad-6045bd8f91dd)
- $select : _msdyn_associatedaccountvendorid_value,_msdyn_associatedaccountid_value,msdyn_contactforpartyid
- $expand : msdyn_associatedaccountid
- $select : accountid,_owningbusinessunit_value,accountnumber
Response :
{ "@odata.context": "foo.crm4.dynamics.com/.../$metadata#...", "@odata.etag": "W/\"52413817\"", "_msdyn_associatedaccountvendorid_value": null, "_msdyn_associatedaccountid_value": "8ec6dbf0-cefd-ec11-82e5-6045bd8accda", "msdyn_contactforpartyid": "1100ed03-d08c-ed11-81ad-6045bd8f91dd", "msdyn_associatedaccountid": { "@odata.etag": "W/\"45904125\"", "accountid": "8ec6dbf0-cefd-ec11-82e5-6045bd8accda", "_owningbusinessunit_value": "1c7e433e-f36b-1410-8fb7-00ab8fe0e9a8", "accountnumber": "C0000008" } }
Note that in both examples, the last $expand is the same, but in first case the accountnumber and _owningbusinessunit_value are null, not in the second case. Is this expected behavior when using nested $expand ? Do you guys have a workaround (without doing two requests) ?