Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Filtering Lookup

(0) ShareShare
ReportReport
Posted on by

Hi Guyz.. 

          I am a beginner working in crm. I just want to filter look up using addpresearch function. In contact i have two fields state and city. I want to filter City based on the state. I have written the code but i am getting error as

unable to get addPresearch function.

function preFilterLookup() {

Xrm.Page.getControl("new_city").addPreSearch(function () {
addLookupFilter();
});

}

function addLookupFilter() {
//check if the city is not empty



var state = Xrm.Page.getAttribute("new_state").getValue();

//create a filter xml


var filter= "<filter type='and'>" +
"<condition attribute='new_state' operator='eq' value='" + state + "' />" +
"</filter>";
//add filter
Xrm.Page.getControl("new_city").addCustomFilter(filter);


}

*This post is locked for comments

  • Suggested answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at
    RE: Filtering Lookup

    Hi Muthuraman,

    Please use below line:

    function preFilterLookup() {

    Xrm.Page.getControl("new_city").addPreSearch(function () {

    this.addLookupFilter();

    });

    }

    Thanks

    Hemant

  • Community Member Profile Picture
    on at
    RE: Filtering Lookup

    18787.Capture.PNG      

    In the bottom you can see State and city. These are the lookup fields. Based on the state city should be filtered.

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: Filtering Lookup

    Sharing screenshots of your field placement over forum, field name could help as well

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Filtering Lookup

    Hi,

                 Are you trying to read the control from the Business Flow section??? or from Form area???

    Best Regards

    Shah

  • Muhammad Salahuddin Profile Picture
    470 on at
    RE: Filtering Lookup

    Please write down the code you have written for below check:

    //check if the city is not empty

  • Community Member Profile Picture
    on at
    RE: Filtering Lookup

    ya. crm 2015 online.

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: Filtering Lookup

    Just to confirm, you are using CRM 2015 right ??

  • Community Member Profile Picture
    on at
    RE: Filtering Lookup

    Actually it is not going inside the function addLookupfilter

  • Suggested answer
    Muhammad Salahuddin Profile Picture
    470 on at
    RE: Filtering Lookup

    "<condition attribute='new_state' operator='eq' value='" + state + "' />" +

    "</filter>";

    if "new_state" is lookup field, then.

    Change above line of your code as:

    "<condition attribute='new_state' operator='eq' value='" + state[0].id + "' />" +

    "</filter>";

    also register preFilterLookup() function on form load event.

  • Community Member Profile Picture
    on at
    RE: Filtering Lookup

    Ya i know even we can do this by this method also. But just for practice i want to do this way.

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics

Product updates

Dynamics 365 release plans