Lookup field with custom filter not working properly in UCI

4.3k Views Asked by At

I have some JS code here that creates a custom filter using some criteria and then adds it to a lookup field on the form. When this code is triggered and ran the first time it works properly; the correct results appear. But if you change the criteria of the custom filter (change one of the fields on the form the createCustomFilter command uses to create the fetchxml), then no results show up in the lookup when there should be results.

This issue only occurs in the new Unified Interface. I have tested this same code in the Web Interface and this issue does not occur; the code runs properly.

My guess is that the previously applied filter is not being removed? That's why no results show up. Is there any workaround that would allow this to work in the UCI?

Please advise.

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

There are 2 best solutions below

0
Aron On

Here's the essence of how we're filtering lookups in both the UCI and Legacy UI in a v9.1 system:

//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);
}

Please note that I adapted this code for simplicity and privacy. I did not test it in its current form.

0
Mohammad Atiour Islam On

You can try this sample code.

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");  
     },              
   }  
 };