2

我这里有一些 JS 代码,它使用某些条件创建自定义过滤器,然后将其添加到表单上的查找字段中。当此代码第一次被触发并运行时,它可以正常工作;出现正确的结果。但是,如果您更改自定义过滤器的条件(更改 createCustomFilter 命令用于创建 fetchxml 的表单上的字段之一),那么当应该有结果时,查找中不会显示任何结果。

此问题仅出现在新的统一接口中。我已经在 Web 界面中测试了相同的代码,但没有出现此问题;代码运行正常。

我的猜测是之前应用的过滤器没有被删除?这就是为什么没有结果显示。是否有任何解决方法可以让它在 UCI 中工作?

请指教。

var filter;

function OnFieldChange(executionContext) {
    var formContext = executionContext.getFormContext();
    if (filter != "" && filter != null) {
        formContext.getControl("test_lookupfield").removePreSearch(lookupCustomFilter);
    }
    filter = createCustomFilter(executionContext);
    formContext.getControl("test_lookupfield").addPreSearch(lookupCustomFilter);
}

function lookupCustomFilter(executionContext) {
    var formContext = executionContext.getFormContext();
    formContext.getControl("test_lookupfield").addCustomFilter(filter);
}

function createCustomFilter(executionContext) {
    //creates a custom fetchxml filter that has been tested and is correct
}
4

2 回答 2

1

以下是我们如何在 v9.1 系统中过滤 UCI 和旧版 UI 中的查找的本质:

//Legacy UI uses custom views, UCI only custom filters
views.push({
    id: '{' + getRandomGuid().toUpperCase() + '}',
    fetchXml: '' +
        '<fetch mapping="logical" distinct="true" version="1.0">' +
            '<entity name="product">' +
                '<attribute name="productid" />' +
                '<attribute name="productnumber" />' +
                '<attribute name="name" />' +
                '<attribute name="description" />' +
                '<order attribute="productnumber" descending="false" />' +          
                '<filter type="and">' +
                    '<condition attribute="new_pricelevelid" operator="eq" value="' + myGuid + '" />' +
                '</filter>';
            '</entity>' +
        '</fetch>',
    layoutXml: '' +
        '<grid name="resultset" object="' + productTypeCode + '" jump="name" select="0" icon="0" preview="0">' +
        '<row name="result" id="productid">' +
        '<cell name="name" width="125" />' +
        '<cell name="description" width="400" />' +
        '</row>' +
        '</grid>',
    name: 'Custom Product View',
    recordType: productTypeCode,
    Type: "0"
});        
var CustomFilter =  '<filter type="and">' +
                        '<condition attribute="new_pricelevelid" operator="eq" value="' + myGuid + '" />' +
                    '</filter>';
try {

    var lookupParameters = {};
    lookupParameters.entityTypes = ['quote'];
    lookupParameters.defaultEntityType = 'quote';
    //lookupParameters.defaultViewId = views[0].id;
    lookupParameters.allowMultiSelect = false;

    //Xrm.Internal.isUci() is unsupported!
    if (Xrm.Internal.isUci() ) {
        //Filter on UCI
        if (CustomFilter != null) {
            lookupParameters.filters = [{ filterXml: CustomFilter }];
        }
    }
    else {
        //Filter on Legacy UI
        lookupParameters.customViews = [views[0]];
        lookupParameters.viewIds = [views[0].id];
        lookupParameters.defaultViewId = views[0].id;
    }

    //Use OOB CRM lookup w/ Custom Filter.
    Xrm.Utility.lookupObjects(lookupParameters).then(
        function (selectedItems) {
            callback.call(scope, ifNull(selectedItems, []));
        },
        function (error) {
            if (error != null) {
                Xrm.Utility.alertDialog(error.message);
            }
        });
} 
catch (e) {
    Xrm.Utility.alertDialog(e.message);
}

请注意,为了简单和隐私,我修改了此代码。我没有以目前的形式对其进行测试。

于 2019-01-11T04:12:47.213 回答
0

你可以试试这个示例代码。

var demoLAB = demoLAB || {};  
var classId;  
 demoLAB.Stuedent = {  
   Form: {  
     Load: function (executionContext) {  
       'use strict';  
       var formContext = executionContext.getFormContext();         
       this.attachEvents(executionContext);  
     },  
     attachEvents: function (executionContext) {  
       'use strict';  
       var formContext = executionContext.getFormContext();  
       var form = demoLAB.Stuedent.Form;         
       // Student Change Event  
       formContext.getAttribute("demo_studentId").addOnChange(form.studentOnChange);         
       
     },  
      
     studentOnChange: function (executionContext) {  
       'use strict';  
       var formContext = executionContext.getFormContext();  
       var form = demoLAB.Stuedent.Form;  
       if (formContext.getAttribute("demo_studentId").getValue() != null) {  
         var studentId = formContext.getAttribute("demo_studentId").getValue()[0].id.slice(1, -1);  
         // Retrive current student current class  
         Xrm.WebApi.retrieveRecord("student", studentId, "?$select=_classId_value").then(  
           function success(studentResult) {  
             classId = studentResult._classId_value;  
             // Add presearch for teacher   
             formContext.getControl("demo_teacherId").addPreSearch(form.filterTeacher);  
           },  
           function (error) {  
           }  
         );  
       }  
     },  
     // Call back function for teacher   
     filterTeacher: function (executionContext) {  
       'use strict';  
       var formContext = executionContext.getFormContext();  
       var teacherFilter = "<filter type='and'><condition attribute='demo_classId' operator='eq' value='" + classId + "'/></filter>";  
       formContext.getControl("demo_teacherId").addCustomFilter(teacherFilter, "teacher");  
     },              
   }  
 };  
于 2021-07-09T10:12:57.827 回答