Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Retrieving entities related entities with details using Web API

Posted on by 245

Hi,

We have a requirement to retrieve an entity (for example, account) and it its associated contacts with its details, then each contact has lookup field that I need to extract the details too (e.g. contactMethod)

For the first call with account, I'm using $expand to contacts, but it seems it is not possible to call another $expand for the lookup of each contact as multiple $expand isn't supported.

Is there an optimal way to retrieve all these information using a single call in Web API?

Currently, I'm looping a call for each contact on the account entity and the performance is not good.

Please advise.

  • Suggested answer
    Retrieving entities related entities with details using Web API
    Please try below to expand 2 different lookups, and field name new_customentity
    https://[OrgURL]/api/data/v9.1/accounts(2901ac3b-d8d8-ea11-a814-000d3a33f24d)?$select=name&$expand=primarycontactid($select=contactid,fullname,new_customentity($select=new_name))
  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Retrieving entities related entities with details using Web API

    Hi Benjamin,

    I'm so happy to hear that you've resolved your problem :)

    Please close this thread so that we could help other users with similar issues.

    Regards,

    Leo

  • Suggested answer
    RE: Retrieving entities related entities with details using Web API

    It seems I found a way using FetchXML instead. See example below

    <fetch top='50' >
    <entity name='mli_policy' >
    <all-attributes/>
    <filter>
    <condition attribute='mli_policyid' operator='eq' value='068bafb7-00da-e911-8144-0050568b0cd5' />
    </filter>
    <link-entity name='mli_policycontactmethod' from='mli_policyid' to='mli_policyid' link-type='outer' alias='PolicyContactMethod' >
    <all-attributes/>
    <link-entity name='mli_contactmethod' from='mli_contactmethodid' to='mli_contactmethodid' link-type='outer' alias='ContactMethod' />
    </link-entity>
    <link-entity name='mli_policyaccountrole' from='mli_policyid' to='mli_policyid' link-type='outer' alias='PolicyAccountRole' >
    <all-attributes/>
    <link-entity name='contact' from='contactid' to='mli_customerid' link-type='outer' alias='Contact' />
    </link-entity>
    <link-entity name='mli_policycertificateaccountrole' from='mli_policyid' to='mli_policyid' link-type='outer' alias='PolicyCertificateAccountRole' >
    <all-attributes/>
    </link-entity>
    <link-entity name='mli_policycertificate' from='mli_policyid' to='mli_policyid' link-type='outer' alias='Certificate' >
    <all-attributes/>
    </link-entity>
    <link-entity name='mli_accountfinancialaccount' from='mli_accountfinancialaccountid' to='mli_accountfinancialaccountid' link-type='outer' alias='AccountFinancialAccount' />
    </entity>
    </fetch>

  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Retrieving entities related entities with details using Web API

    Hi Partner,

    Unfortunately, $expand is only allowed to use one time in each web api, if we use multiple times, we will get error "Query option '$expand' was specified more than once, but it must be specified at most once."

    For example I test to get multiple related entities from account and it failed.

    https://[OrgURL]/api/data/v9.1/accounts(2901ac3b-d8d8-ea11-a814-000d3a33f24d)?$select=name&$expand=primarycontactid($select=contactid,fullname&$expand=new_customentity($select=new_name))

    pastedimage1601263078185v1.png

    May I know where you are using this web api? In D365 form or outside D365? 

    If you are using Web Api in D365 forms, you could use JS code to call web api for multiple relationships query, it is very easy with "Xrm.page.webapi.retrievemultiplereocrds".

    If you are using Web Api outside of D365, I suggest you to use fetch Xml instead.

    Regards,

    Leo

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans