web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Martin Donnelly Profile Picture
    1,030 on at

    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?

  • Martin Donnelly Profile Picture
    1,030 on at

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

  • Suggested answer
    Community Member Profile Picture
    on at

    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).

     

     

     

  • Suggested answer
    Community Member Profile Picture
    on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans