Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

While filtering lookup, addCustomFilter doesn't seem to work if I change the field of search in fetchXML (filter)

Posted on by Microsoft Employee

I am making a generic function to filter City based on State entry and if State field is null, City should be filtered for entered country.

I have bound my addPreSearch to function onCityPreSearch() which is below.

onCityPreSearch = function (formContext) {
    var stateid = this.readGuid(formContext, this._addressFields.fieldState);
    var countryid = this.readGuid(formContext, this._addressFields.fieldCountry);
    var cityCtrl1 = formContext.getControl(this._addressFields.fieldCity);
    var fetchXML = "";

    if (stateid) {
      fetchXML = "";
    } else if (countryid) {
      fetchXML = "";
    } else {
      fetchXML = "";
    }

    fetchXML = ""   fetchXML   "";
    
    cityCtrl1.addCustomFilter(fetchXML);
  };

The scenarios 1 to 3 below are working, 4th doens't work

  1. The first time form loads and I select a country, it filters city for that Country
  2. After that, if I select state, it filters city for entered State
  3. If I change the state, the cities of the changed State are correctly shown
  4. Now, if I delete the State value, I expect, it should filter and show cities from the entered Country - this doesn't work - it continues to show the cities of the last value I had entered for the State.

What correction do I need to make?

EDiT - 

The below screenshot displays the values that are assigned at run time -

the javsacript code screenshot at runtime

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: While filtering lookup, addCustomFilter doesn't seem to work if I change the field of search in fetchXML (filter)

    I think this is some problem with CRM itself.. I could make it work with a Hack instead, only when I molded my search using stateid as filter, it works.. see the 

    if (stateid) {
      fetchXML = "";
    } else if (countryid) {
      fetchXML = ""  
                "";
    } else {
      fetchXML = "";
    }

    However, suggestions are still invited if there is something wrong in my approach, pl suggest.

  • Naveed Alizai Profile Picture
    Naveed Alizai 5 on at
    RE: While filtering lookup, addCustomFilter doesn't seem to work if I change the field of search in fetchXML (filter)

    Hi Avdhut,

    There is a similar question answered on another thread. Go through the answers section, there is a way of reseting/removing customFilter which may help you in your scenario. I know from the title it looks like they are removing the filter but in some answers they are doing what you need.

    community.dynamics.com/.../how-to-remove-the-custom-filter-added-to-a-lookup

    Hope it helps.

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