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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Retrieving entities related entities with details using Web API

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

I have the same question (0)
  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    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

  • Suggested answer
    Benjamin Fallar III Profile Picture
    245 on at

    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>

  • LeoAlt Profile Picture
    16,331 Moderator on at

    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
    Community member Profile Picture
    2 on at
    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))

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 182 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 123

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans