简体   繁体   中英

Getting all related Contact for an Account entity in Dynamics 365 CRM v9.0 web api

Basically, I'm querying the D365 web API and I'm trying to get all of the related contacts for an account.

Been following this link:

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/query-data-web-api#retrieve-related-entities-by-expanding-navigation-properties

Trying to use $expand but it only will bring over _primarycontactid_value . So just the one primary contact and not everyone that is related to the Account.

It would look something like the following:

/api/data/v9.0/accounts&?select=name&$expand=Contacts(fullname, email)

The only fields in Account that have "contact" in them are:

preferredcontactmethodcode
_primarycontactid_value
address2_primarycontactname
address1_primarycontactname
_tcc_primaryinvoicecontactid_value
_tcc_consultingcontact_value
_tcc_contactlist_value //some custom field that doesn't apparently do anything
_new_foundationcontact_value
_tcc_primaryapcontactid_value

So none of which can be used to look-up all of the contacts... that I know of.

Another way to do it would be to start with the Contact first and then $expand= on the _parentcustomerid_value . But I need to filter on the Account to specify certain accounts that I want... this would just bring over every account and be incredibly slow. I'm not sure there is a way to $filter= on an $expand= value.

So:

  1. How can I query the Account and all the related to Contacts for an Account?
  2. If there is no way, is it possible to use $filter= on and $expand= value?

Trying to keep the amount of queries to a minimum. This could be solved by doing multiple iterative queries, but that will just make it incredibly slow. Or just query everything and piece it together, but that will be slow as well.

Ok, stumbled across the answer: contact_customer_accounts .

Query ends up looking like the following: /api/data/v9.0/accounts&?select=name&$expand=contact_customer_accounts($select=fullname) .

As far as I can tell, custom relationships can be used as well, although I have only tested with 1:N types.

Still takes a little while to generate, but works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM