Scenario: There is a portal which shows data in grid and this data comes from Dynamics 365. Being Dynamics developed I have been requested to provide a query which will show data in below format
for Example, Phone call can be associate with either Contact or CustomAccount(Custom Entity) and CustomAccount has lookup of Contact on its form. We require all the Phone Call records but there is a catch. When fetching Phone Call for contact, we need to fetch field value of contact like Residential Address, Mailing Address, Email Address, Phone Numbers. it is doable. But if the Phone call associated with CustomAccount entity, then we have to fetch contact associated with CustomAccount Entity and then its fields like Residential Address, Mailing Address, Email Address, Phone Numbers.
I tried building query but its result is weird and showing only Phone Calls associated with CustomAccount entity and not contact entity.
<fetch>
<entity name='phonecall' >
<all-attributes/>
<link-entity name='contact' from='contactid' to='regardingobjectid' link-type='outer' alias='contact' >
<all-attributes/>
</link-entity>
<link-entity name='pref_CustAccount' from='pref_CustAccountid' to='regardingobjectid' link-type='outer' alias='account' >
<link-entity name='contact' from='contactid' to='lpl_clientid' link-type='inner' alias='accountcontact' >
<all-attributes/>
</link-entity>
</link-entity>
</entity>
</fetch>
I just replicated your scenario on my CRM instance. I think your problem is with Link type for below line.
<link-entity name='contact' from='contactid' to='lpl_clientid' link-type='inner' alias='accountcontact' >
you need outer rather than inner. When I gave inner I got only those records related to above line.
Here is my complete fetch
<fetch>
<entity name="phonecall" >
<link-entity name="contact" from="contactid" to="regardingobjectid" link-type="outer" alias="directcontact" >
<attribute name="fullname" />
</link-entity>
<link-entity name="account" from="accountid" to="regardingobjectid" link-type="outer" alias="account" >
<link-entity name="contact" from="contactid" to="primarycontactid" link-type="outer" alias="primarycontact" >
<attribute name="fullname" />
</link-entity>
</link-entity>
</entity>
</fetch>