Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

INLINE LOOKUP: addPreSearch & addCustomFilter

Posted on by Microsoft Employee

A little background info:

- I have a Customer (customerid) lookup field where the user selects a parent account.

- I have a Location (ars_location) lookup field where the user selects a child account of the parent account.

--------------------------------------------------------------------------------------------

I am currently trying to add a custom filter to my Customer field so that only parent accounts show up in the results. I have created a view so I first tried this,

function setLookupView()
{
    Xrm.Page.getControl("customerid").setDefaultView("{B9300D26-1337-E611-80EA-C4346BDCF181}");
}


But what this does is filter the accounts after I click the "View More Records" button and does not filter in the the inline results.

--------------------------------------------------------------------------------------------

Next, I tried adding a presearch/customfilter to it. This is what I did:

function parentFilterLookup() {
//Check if the control exist on the form
   if (Xrm.Page.getControl("header_process_customer") != null) {
      // add the event handler for PreSearch Event
      Xrm.Page.getControl("header_process_customer").addPreSearch(addFilter);
   }
}

function addFilter() {
   var fetchQuery;
   try {
      //Build fetch
      fetchQuery = "<filter type='and'>" +
      "<condition attribute='parentcustomerid' operator='null' />" + "</filter>";
      //add custom filter
      Xrm.Page.getControl("header_process_customer").addCustomFilter(fetchQuery);
   } 
   catch (e) {
         Xrm.Utility.alertDialog("addFilter Error: " + (e.description || e.message));
   }
}


I have also tried replacing "header_process_customer" with "customerid". Both to no success.

--------------------------------------------------------------------------------------------

What's strange is that I already have a working presearch/customfilter on a different lookup field on the same form. This is the code for that:

function filterLookup() {
//Check if the control exist on the form
   if (Xrm.Page.getControl("header_process_ars_location") != null) {
      // add the event handler for PreSearch Event
      Xrm.Page.getControl("header_process_ars_location").addPreSearch(addFilter);
   }
}

function addFilter() {
   var customerID = null;
   var customerLookup;
   var fetchQuery;
   try {
      //Check if control exist on form
      if (Xrm.Page.getControl("header_process_customer") != null &&  Xrm.Page.getControl("header_process_customer").getAttribute().getValue() != null) {
         //Get Account lookup value
         customerLookup = Xrm.Page.getControl("header_process_customer").getAttribute().getValue();
         //Get the account id
         customerID = customerLookup[0].id;
      }
      //Build fetch
      if (customerID != null || customerID != undefined) {
         fetchQuery = "<filter type='and'>" +
         "<condition attribute='parentcustomerid' operator='eq' value='" + customerID + "' />" + "</filter>";
         //add custom filter
         Xrm.Page.getControl("header_process_ars_location").addCustomFilter(fetchQuery);
      }
   } 
   catch (e) {
         Xrm.Utility.alertDialog("addFilter Error: " + (e.description || e.message));
   }
}

What could I be doing wrong? Any suggestions?

*This post is locked for comments

  • Suggested answer
    sandeepstw Profile Picture
    sandeepstw 4,601 on at
    RE: INLINE LOOKUP: addPreSearch & addCustomFilter

    Hi,

    You are adding same function to same control two times so it will be override with last function added on form.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: INLINE LOOKUP: addPreSearch & addCustomFilter

    They are on the same form but the functions are implemented in separate libraries. I tried renaming them but it did nothing.

  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    Nadeeja Bomiriya 6,804 on at
    RE: INLINE LOOKUP: addPreSearch & addCustomFilter

    Hi Brian,

    You have two addFilter() functions here.  Are both on the same form?  Try renaming them to addFilterCustomer() and addFilterLocation().

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans