Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

JavaScript addPreSearch() filter on customer lookup not working in v9 for multiple filters (9.0.3.7)

Posted on by Microsoft Employee

I have a field dummy_inspector that has the customer type.

When a field dummy_inspector is from type account, then a grid can show another inspectors. In a lookup I want to filter on all those inspectors.

Actually the customerAddressFilter would be this:

 

var customerAddressFilter = "<filter type='or'>

 <condition attribute='dummy_customer' operator='eq' value='{39D2DD83-835F-E911-B818-0050569C84C7}' />

 <condition attribute='dummy_customer' operator='eq' value='{C0379546-835F-E911-B818-0050569C84C7}' />

</filter>";

 

Is this possible? It looks like only the first condition is executed.

I have written the following JavaScript to accomplish this

 

if (FormContext.getAttribute("dummy_inspector").getValue() != null) {

 

            var inspectorId = FormContext.getAttribute("dummy_inspector").getValue()[0].id;

            var inspectorType = FormContext.getAttribute("dummy_inspector").getValue()[0].entityType;

            var condition = "";

 

            if (inspectorType.toLowerCase() == "account") {

                // Get accountid(s) from inspection sites

                var inspectionsitesGrid = FormContext.getControl("listInspectionSites").getGrid();

                var inspectionsitesRows = inspectionsitesGrid.getRows();

                var rowCount = inspectionsitesGrid.getTotalRecordCount();

                for (var i = 0; i < rowCount; i++) {

                    var rowEntity = inspectionsitesRows.get(i).getData().getEntity();

                    condition += "<condition attribute='dummy_customer' operator='eq' value='" + rowEntity.getKey() + "' />";

                }

            }

 

            var lookupControl = FormContext.getControl("dummy_inspectionlocation");

            if (lookupControl != null) {

                var customerAddressFilter = "<filter type='or'><condition attribute='dummy_customer' operator='eq' value='" + inspectorId + "' />" + condition + "</filter>";

                lookupControl.addPreSearch(function () { lookupControl.addCustomFilter(customerAddressFilter); });

            }

}

 

 

 

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 for multiple filters (9.0.3.7)

    To avoid having the timeouts when checking if the subgrid is loaded, i managed it to do by querying the N:N relationship data by using the xrmservicetoolkit.

    This way I'm sure that i read all the records in the subgrid, apply them to the filter and pass that filter to the lookup.  

    Then the lookup is always containing the correct filter linked to the subgrid.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 for multiple filters (9.0.3.7)

    I already tried with the IN operator, same result, the grid record is not showing in the lookup.

    if (FormContext.getAttribute("dummy_inspector").getValue() != null) { 

                var inspectorId = FormContext.getAttribute("dummy_inspector").getValue()[0].id;            

                var inspectorType = FormContext.getAttribute("dummy_inspector").getValue()[0].entityType;            

                var condition = ""; 

                if (inspectorType.toLowerCase() == "account") { 

                    // Get accountid(s) from inspection sites  
                    var inspectionsitesGrid = FormContext.getControl("listInspectionSites").getGrid();                

                    var inspectionsitesRows = inspectionsitesGrid.getRows();                

                    var rowCount = inspectionsitesGrid.getTotalRecordCount();

                    for (var i = 0; i < rowCount; i++) {                    

                             var rowEntity = inspectionsitesRows.get(i).getData().getEntity();                     

                             condition += "<value>" + rowEntity.getKey() + "</value>";                                   

                    }            

              }

              var lookupControl = FormContext.getControl("dummy_inspectionlocation");            

              if (lookupControl != null) {

                    var customerAddressFilter = "<filter type='and'><condition attribute='dummy_customer' operator='in'><value>" + inspectorId + "</value>" + condition + "</condition></filter>";                           

                    lookupControl.addPreSearch(function () { lookupControl.addCustomFilter(customerAddressFilter); });

                }

    }

     

    But, I know the reason why the grid records were missing in the lookup.

     

    The rowcount ALWAYS was 0.

     

    So I placed this function in my onload:

     

     getTotalGridRecordCount: function() {
            debugger;
            var functionName = "onLoad";
            try {
                setTimeout(function () {
                    if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("listInspectionSites") != null && Xrm.Page.getControl("listInspectionSites") != undefined) {
                        var count = Xrm.Page.getControl("listInspectionSites").getGrid().getTotalRecordCount();
                       
          SetLookupFilter();
                    }
                }, 5000);
            }
            catch (e) {
                Xrm.Utility.alertDialog(functionName + "Error: " + e.message || e.description);
            }
        },

     

    With SetLookupFilter having the code from earlier on.

     

    So, after 5 seconds the grid including the records were loaded so I could build the lookupfilter.

     

    BUT ! Then I got a second problem, the grid records were still missing in the lookup, despite the fact that the lookup filter was containing it.

     

    Reason for this is that when the lookup filter is loaded, a override of it is not working. It's not refreshing the filter of the lookup.

     

    So what have I done?

     

    I changed my code so that in case of a onload I wait for 5 seconds so I'm sure the grid is loaded including the records, and then my lookup filter was containing these records.

     

    And when I add records to the N:N subgrid, I reload my form, so 5 seconds later my lookup filter is containing that new record(s).

     

     

     

  • Martin Donnelly Profile Picture
    Martin Donnelly 1,010 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 for multiple filters (9.0.3.7)

    ps- I've been trying this with no success: community.dynamics.com/.../editable-grids-lookup-field-custom-filter

  • Martin Donnelly Profile Picture
    Martin Donnelly 1,010 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 for multiple filters (9.0.3.7)

    I kind of got tired reading the jscript and grids tend to be a mystery but, wouldn't your filter have to be operator 'in' with a list of options?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans