Dynamics 365中JScript/FetchXML关联实体过滤字段报错求助
Dynamics 365中JScript/FetchXML关联实体过滤字段报错求助
嘿,我看了你的问题和代码,应该是FetchXML里的实体关联逻辑出问题了,导致系统识别不到对应的字段。咱们一步步来理清楚:
先明确你的需求(再核对一遍):
你要给msdyn_customerasset表单上的uneeti_agreementinvoiceproduct字段做过滤,只显示满足以下两个条件的msdyn_agreementinvoiceproduct记录:
- 该记录所属合同(
msdyn_agreement)的客户(msdyn_serviceaccount)等于当前资产的账户(msdyn_account) - 该记录的产品(
msdyn_product)等于当前资产的产品(msdyn_product)
你的代码里的问题:
你错误地在FetchXML里关联了msdyn_customerasset实体,并且关联字段完全不对——msdyn_customerasset和msdyn_agreementinvoiceproduct之间并没有直接通过msdyn_customerassetid和msdyn_agreementinvoiceproductid关联的关系,而且你在这个错误的关联里过滤msdyn_product,系统自然会报错说字段不属于对应表。
其实你的第二个条件根本不需要关联msdyn_customerasset,直接在msdyn_agreementinvoiceproduct实体上过滤msdyn_product等于当前资产的产品ID就可以了。
修正后的代码:
function filterProductContratFS(context) { var formContext = context.getFormContext(); var fieldRecipientCall = formContext.getAttribute("msdyn_account").getValue(); var fieldRecipientCall2 = formContext.getAttribute("msdyn_product").getValue(); if (fieldRecipientCall && fieldRecipientCall2) { // 注意:Dynamics 365返回的ID带大括号,FetchXML需要纯ID字符串,所以要替换掉 var accountId = fieldRecipientCall[0].id.replace("{", "").replace("}", ""); var productId = fieldRecipientCall2[0].id.replace("{", "").replace("}", ""); console.log(accountId); console.log(productId); var fetchXML = ` <fetch> <entity name='msdyn_agreementinvoiceproduct'> <!-- 关联合同实体,过滤合同的客户等于当前资产的账户 --> <link-entity name='msdyn_agreement' from='msdyn_agreementid' to='msdyn_agreement' alias='agreement'> <filter> <condition attribute='msdyn_serviceaccount' operator='eq' value='${accountId}' uitype='account'/> </filter> </link-entity> <!-- 直接过滤当前实体的产品等于当前资产的产品 --> <filter> <condition attribute='msdyn_product' operator='eq' value='${productId}' uitype='product'/> </filter> </entity> </fetch>`; var agreementInvoiceProductControl = formContext.getControl("uneeti_agreementinvoiceproduct"); // 先移除之前的过滤,避免重复添加导致逻辑混乱 agreementInvoiceProductControl.removePreSearch(filterProductContratFS); agreementInvoiceProductControl.addPreSearch(function () { agreementInvoiceProductControl.addCustomFilter(fetchXML); }); } else { // 如果账户或产品为空,清空过滤条件 var agreementInvoiceProductControl = formContext.getControl("uneeti_agreementinvoiceproduct"); agreementInvoiceProductControl.removePreSearch(filterProductContratFS); agreementInvoiceProductControl.clearCustomFilters(); return; } }
几个关键的修正点:
- 移除了错误的
msdyn_customerasset关联:你的第二个条件不需要关联资产表,直接过滤msdyn_agreementinvoiceproduct的产品字段即可 - 处理了ID的大括号:Dynamics 365中通过getAttribute获取的ID自带
{},而FetchXML要求纯ID字符串,必须替换掉 - 添加了过滤清理逻辑:避免多次触发时重复添加过滤条件,导致筛选结果异常
- 给关联实体加了别名:让FetchXML结构更清晰,后续扩展需求时更方便维护
你可以试试这个修正后的代码,应该就能解决报错问题了。如果还有其他特殊情况(比如实体间有自定义关联关系),可以再补充说明~
备注:内容来源于stack exchange,提问作者Christophe Da Silva
相关产品推荐
相关产品推荐

