web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Null fields with second level OData $expand

(0) ShareShare
ReportReport
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) ?

I have the same question (0)
  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    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.

  • JuniorD Profile Picture
    38 on at

    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
    JuniorD Profile Picture
    38 on at

    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

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Great. Good to hear that you got it.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans