Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Null fields with second level OData $expand

Posted on by 38

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

Categories:
  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 Moderator on at
    RE: Null fields with second level OData $expand

    Great. Good to hear that you got it.

  • Verified answer
    JuniorD Profile Picture
    JuniorD 38 on at
    RE: Null fields with second level OData $expand

    Ok I think I found it: learn.microsoft.com/.../retrieve-entity-using-web-api (see the "Note" block)

    If you use nested $expand on collection-valued navigation properties, only the first level of data will be returned.

    I found it thanks to this post: https://stackoverflow.com/questions/60229203/odata-multi-level-query-to-cds-returns-strange-results-at-the-3rd-level

    Similar post in D365 forums: community.dynamics.com/.../odata-query-response

  • JuniorD Profile Picture
    JuniorD 38 on at
    RE: Null fields with second level OData $expand

    Thanks for the response.

    Do you have a link to the documentation ?

    I have not seen a documentation saying it's forbidden to have nested $expand. And some people are using it. See links below:

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 Moderator on at
    RE: Null fields with second level OData $expand

    Hello JuniorD,

    Based on the documentation, the $expand operator allows you to retrieve related data in a single query and it supports single level expand only, which means that you can't use nested expands or multiple expands on the same level.

    A workaround for this issue is to make multiple requests to the API and retrieve the data in multiple steps. This would involve making a request to retrieve the parties of the contact and then making another request to retrieve the account information for each party.

    Another option would be to use the Microsoft Dynamics 365 Web API which provides more advanced querying capabilities and supports nested expands.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,558 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,645 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans