In this Blog, we will learn how we can use fetchxml with link-entity in Web API.
What is Link-Entity
- To combine data from different records in your query you need to use the <link-entity> element.
2. Each <link-entity> needs 3 key pieces of information:
2.a. The entity types to link to
2.b. The attribute in the main entity to join from
2.c. The attribute in the another entity to join to
NOTE: If you have a many-to-many (N:N) relationship you can’t join the entities directly, you have to join via the intermediate “intersect” entity.
3. There are two types of link-types: inner and outer.
4. The Link-Entity syntax requires providing of an Alias using which the values of the attributes can be accessed from the result-set.
NOTE: If you do not provide an alias, the platform will auto generate the alias for you.
Example:
Suppose we have a requirement in which we need contact details like fullname, firstname, lastname and the account details like name and phone number that is related to contact.
Solution:
- First, we will write a simple FetchXml query for contact in which we will select the attributes like fullname, firstname, lastname.
API Request:
https://dev-onepiece07.api.crm7.dynamics.com/api/data/v9.2/contacts?fetchXml=<fetch> <entity name="contact"> <attribute name="fullname" /> <attribute name="lastname" /> <attribute name="firstname" /> </entity></fetch>
Output:
2. As per the requirement that has been mentioned in the example, we will add the link- entity to get the account and its details like name and phone number that are related to contact.
2.a. We will use an inner link-type which will match the data of both the entity, and it will give the result.
API Request:
https://dev-onepiece07.api.crm7.dynamics.com/api/data/v9.2/contacts?fetchXml=<fetch> <entity name="contact"> <attribute name="fullname" /> <attribute name="lastname" /> <attribute name="firstname" /> <filter> <condition attribute="statecode" operator="eq" value="0" /> </filter> <link-entity name="account" from="accountid" to="parentcustomerid" link-type="inner" alias="account"> <attribute name="name" /> <attribute name="telephone1" /> </link-entity> </entity></fetch>
Output:
2.b. We will use an outer link-type which will give all the data of the first entity if there are no matching records.
API Request
https://dev-onepiece07.api.crm7.dynamics.com/api/data/v9.2/contacts?fetchXml=<fetch> <entity name="contact"> <attribute name="fullname" /> <attribute name="lastname" /> <attribute name="firstname" /> <filter> <condition attribute="statecode" operator="eq" value="0" /> </filter> <link-entity name="account" from="accountid" to="parentcustomerid" link-type="outer" alias="account"> <attribute name="name" /> <attribute name="telephone1" /> </link-entity> </entity></fetch>
Output:
NOTE: Each query can contain a maximum of 10 links
We have used the link-entity to fetch all contact details like fullname, firstname, lastname and the account details like name and phone number that is related to contact.
The post How to use FetchXml with link-entity in WebAPI D365 CE appeared first on Nebulaa IT Solutions.
*This post is locked for comments