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

点击“高级查找”后Lookup字段自定义视图消失且addCustomFilter无效

Dynamics 365 Lookup筛选问题解决方案

问题1:自定义视图在高级查找后失效

你当前仅在Form OnLoad时添加自定义视图的方式,只会在表单首次加载时生效。使用高级查找操作后,Lookup控件的视图配置会被系统重置,导致自定义视图丢失。

解决方案:通过addPreSearch绑定自定义视图逻辑

将添加自定义视图的代码放到addPreSearch的回调函数中,这样每次打开Lookup(包括高级查找返回后再次打开)时,都会重新应用自定义视图,避免被系统重置。

示例代码:

setValues: function(executionContext) {
    var formContext = executionContext.getFormContext();
    // 绑定预搜索事件,每次打开Lookup前执行
    formContext.getControl("lookupfield").addPreSearch(function() {
        var viewId = formContext.getControl("lookupfield").getDefaultView();
        // 重新注入自定义视图
        formContext.getControl("lookupfield").addCustomView(
            viewId, 
            "entityname", 
            "viewname", 
            fetchXML, 
            layoutXML, 
            true
        );
    });
}

问题2:addCustomFilter无法生效

无报错但筛选无效,大概率是以下几个细节出了问题:

1. Filter格式错误

addCustomFilter要求传入的是单独的<filter>节点XML,不能包含完整的<fetch>或<entity>结构。正确示例:

<filter type="and">
    <condition attribute="statuscode" operator="eq" value="1" />
</filter>

如果传入的是包含<fetch>标签的完整FetchXML,筛选逻辑会直接失效。

2. 实体名称使用错误

addCustomFilter的第二个参数必须是Lookup关联实体的逻辑名称(全小写,比如account而非Account),注意大小写敏感。

3. FormContext作用域问题

你的filterValues函数直接使用formContext,如果该变量是setValues内的局部变量,filterValues会因作用域问题无法访问到,导致代码未实际执行。建议用闭包将formContext传入回调:

修正后的示例代码:

setValues: function(executionContext) {
    var formContext = executionContext.getFormContext();
    formContext.getControl("attributename").addPreSearch(function() {
        // 直接在回调内定义filter,确保作用域正确
        var filter = '<filter type="and"><condition attribute="yourattribute" operator="eq" value="yourvalue" /></filter>';
        formContext.getControl("attributename").addCustomFilter(filter, "entitylogicalname");
    });
}

额外检查点

  • 确认filter中的属性名称是实体的逻辑名称,而非显示名称
  • 如果是多实体Lookup,需确保第二个参数指定了对应的目标实体逻辑名称
  • 可在浏览器控制台打印filter和formContext.getControl("attributename"),确认变量都已正确获取

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 16:10:05