Dynamics 365 FetchXML多关联实体查询仅返回部分记录求助
解决Dynamics 365 FetchXML只返回关联CustomAccount的电话记录问题
我来帮你搞定这个问题!你的FetchXML查询只返回关联CustomAccount的电话记录,主要是两个关键问题导致的:
问题根源
- 多态查找字段未指定实体类型:
regardingobjectid是多态字段(可关联多个实体),你在关联Contact和CustomAccount时没有明确指定entity属性,导致系统无法正确匹配关联Contact的电话记录。 - Inner链接过滤了部分记录:CustomAccount下关联Contact的子链接用了
link-type='inner',这会强制要求CustomAccount必须关联Contact才会返回对应的电话记录,同时也间接干扰了关联Contact的电话记录的返回逻辑。
修正后的FetchXML
<fetch> <entity name='phonecall' > <all-attributes/> <!-- 关联Contact的电话记录,指定entity属性明确匹配多态字段 --> <link-entity name='contact' from='contactid' to='regardingobjectid' link-type='outer' alias='contact' entity='contact'> <all-attributes/> </link-entity> <!-- 关联CustomAccount的电话记录,指定entity属性 --> <link-entity name='pref_CustAccount' from='pref_CustAccountid' to='regardingobjectid' link-type='outer' alias='account' entity='pref_CustAccount'> <!-- 改为outer链接,确保CustomAccount无关联Contact时也能返回电话记录 --> <link-entity name='contact' from='contactid' to='lpl_clientid' link-type='outer' alias='accountcontact' > <all-attributes/> </link-entity> </link-entity> </entity> </fetch>
关键修改说明
- 添加
entity属性:在两个关联到regardingobjectid的link-entity标签中分别添加entity='contact'和entity='pref_CustAccount',这样系统就能准确识别多态字段对应的实体类型,正确关联Contact类型的电话记录。 - 将Inner改为Outer链接:把CustomAccount下的Contact子链接从
inner改成outer,这样即使某个CustomAccount没有关联Contact,对应的电话记录依然会被返回,只是关联的Contact字段值为null,完全符合你“获取所有电话记录”的需求。
可选优化(提升性能)
如果你不需要所有属性,建议替换<all-attributes/>为具体需要的字段,比如:
<attribute name='address1_line1' /> <!-- 住宅地址 --> <attribute name='address2_line1' /> <!-- 邮寄地址 --> <attribute name='emailaddress1' /> <!-- 邮箱地址 --> <attribute name='telephone1' /> <!-- 电话号码 -->
这样可以减少查询返回的数据量,显著提升查询效率。
内容的提问来源于stack exchange,提问作者Nachiket
相关产品推荐
相关产品推荐

