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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to apply Dynamic Filter on Lookup Field using JavaScript in Dynamics 365?

(0) ShareShare
ReportReport
Posted on by 256

Hi All,

I want to apply Dynamic filter on Lookup Field using JavaScript in Dynamics 365.

I have tried below code but its not working in Dynamics 365 :

function filterlookup(){

if(Xrm.Page.getControl("name") != null && Xrm.Page.getControl("name") != undefined){
Xrm.Page.getControl("parentaccountid").addPreSearch(function ()){
var fetchQuery = "<filter type="and"><condition attribute="name" operator="like" value="A%" /></filter>"
Xrm.Page.getControl("parentaccountid").addCustomFilter(fetchQuery);
});
}

}

Please suggest any good solution.

Thank You. 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rajkumar Rajaraman Profile Picture
    on at
  • AnkitSinghal Profile Picture
    256 on at

    I have already tried to implement that code but its not working in Dynamics 365 Online.

  • mvilas02 Profile Picture
    356 on at

    Hi ,

    What is the version of CRM online you are using ? 8.0 / 8.1 / 8.2 ? i think this code will only work in 8.1 and ahead.

    I had the same problem

    Regards,

    Vilas

  • AnkitSinghal Profile Picture
    256 on at

    Hi Vilas,

    I am using 8.2 version of dynamics CRM online.

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    It seems like your code is fine. Are you getting any errors?

    The following code works for me in version 8.2, maybe something I missed:

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

               addActionTypeLookupFilter(recordTypeCode);

           });

    function addActionTypeLookupFilter(recordTypeCode)

    {

       var fetchXml = "<filter type='and'><condition attribute='bgx_recordtypecode' operator='eq' value='" + recordTypeCode + "' /></filter>";

       Xrm.Page.getControl("bgx_changerequesttypeid").addCustomFilter(fetchXml);

    }

  • Suggested answer
    ashlega Profile Picture
    34,477 on at

    Hi,

     it seems to be a syntax error. Try this:

    function filterlookup(){

    if(Xrm.Page.getControl("name") != null && Xrm.Page.getControl("name") != undefined){
    Xrm.Page.getControl("parentaccountid").addPreSearch(function (){
    var fetchQuery = "<filter type='and'><condition attribute='name' operator='like' value='A%' /></filter>";
    Xrm.Page.getControl("parentaccountid").addCustomFilter(fetchQuery);
    });
    }
    }

  • Gokul kansagara Profile Picture
    65 on at

    You Can try this, It's working properly.

    if (typeof (OpportunityJS) == "undefined") { OpportunityJS = { __namespace: true }; }

    OpportunityJS.Opportunity = {

     OnOpportunityLoad: function (executionContext) {

       var formContext = executionContext.getFormContext();

       var fetchXML = "<filter type=\"and\">" +

         "<condition attribute=\"address1_city\" operator=\"eq\" value=\"New York\" />" +

         "</filter>";

       formContext.getControl("parentaccountid").addCustomFilter(fetchXML);

     },

     OnAddPreSearchToAccountLoad: function (executionContext) {

       var formContext = executionContext.getFormContext();

       formContext.getControl("parentaccountid").addPreSearch(OpportunityJS.Opportunity.OnOpportunityLoad);

     }

    }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Varsha deshpande Profile Picture

Varsha deshpande 5

#2
JS-09031509-0 Profile Picture

JS-09031509-0 3

#3
Ciprian  P Profile Picture

Ciprian P 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans