You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dynamics 365中addCustomFilter函数失效问题排查求助

Lookup自定义筛选addCustomFilter不生效问题排查与修复

尝试在Lookup属性上应用自定义筛选,FetchXML单独执行可返回正确值,但addCustomFilter函数却不生效,且已关闭相关记录筛选功能。函数可正常触发且无报错,代码如下:

function filterClassificationValues() {
    var fetchXML = "<filter type='and'><condition attribute='db_mailboxname' operator='eq' value='{guid of the record}' /></filter>";
    formContext.getControl("nameOfTheField").addCustomFilter(fetchXML, "entityName");
}

// 表单加载时调用
function AddPreSearch(executionContext) {
    formContext = executionContext.getFormContext();
    formContext.getControl("nameOfTheField").addPreSearch(filterClassificationValues);
}

排查与修复要点

  • 实体名称必须匹配逻辑名:addCustomFilter的第二个参数entityName必须是Lookup目标实体的逻辑名称(区分大小写),不能使用显示名称。比如目标实体逻辑名为db_classification,这里就不能写错。
  • 动态获取当前记录GUID:硬编码的{guid of the record}可能存在格式错误,建议动态获取当前表单记录ID,代码修改如下:
    function filterClassificationValues() {
        var recordId = formContext.data.entity.getId();
        var fetchXML = `<filter type='and'><condition attribute='db_mailboxname' operator='eq' value='${recordId}' /></filter>`;
        formContext.getControl("nameOfTheField").addCustomFilter(fetchXML, "entityName");
    }
    
  • 验证Lookup控件逻辑名:确认getControl("nameOfTheField")中的字段名称是Lookup字段的逻辑名称,检查表单字段配置确保拼写完全一致。
  • 尝试完整FetchXML结构:部分场景下仅传递<filter>标签可能出现兼容性问题,改用完整FetchXML结构测试:
    var fetchXML = `<fetch><entity name='entityName'><filter type='and'><condition attribute='db_mailboxname' operator='eq' value='${recordId}' /></filter></entity></fetch>`;
    
  • 清除浏览器缓存测试:旧代码缓存可能导致修改不生效,清除缓存后用无痕模式打开表单验证。
  • 排查PreSearch函数冲突:如果该Lookup注册了多个PreSearch函数,暂时移除其他函数单独测试当前逻辑,确认是否存在覆盖或冲突。

内容的提问来源于stack exchange,提问作者SristiJain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 07:15:02