具有自定义过滤器的查找字段在UCI中无法正常工作

时间:2019-01-10 16:51:58

标签: javascript dynamics-crm microsoft-dynamics dynamics-crm-365-v9 dynamics-crm-uci

我这里有一些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
}

2 个答案:

答案 0 :(得分: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);
}

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

答案 1 :(得分: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");  
     },              
   }  
 };