Skip to main content

Notifications

Announcements

No record found.

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

  • Gokul kansagara Profile Picture
    Gokul kansagara 65 on at
    RE: How to apply Dynamic Filter on Lookup Field using JavaScript in Dynamics 365?

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

     }

    }

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to apply Dynamic Filter on Lookup Field using JavaScript in Dynamics 365?

    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);
    });
    }
    }

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: How to apply Dynamic Filter on Lookup Field using JavaScript in Dynamics 365?

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

    }

  • AnkitSinghal Profile Picture
    AnkitSinghal 256 on at
    RE: How to apply Dynamic Filter on Lookup Field using JavaScript in Dynamics 365?

    Hi Vilas,

    I am using 8.2 version of dynamics CRM online.

  • mvilas02 Profile Picture
    mvilas02 356 on at
    RE: How to apply Dynamic Filter on Lookup Field using JavaScript in Dynamics 365?

    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
    AnkitSinghal 256 on at
    RE: How to apply Dynamic Filter on Lookup Field using JavaScript in Dynamics 365?

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

  • Suggested answer
    RE: How to apply Dynamic Filter on Lookup Field using JavaScript in Dynamics 365?

    Refer this:

    www.inogic.com/.../apply-custom-filter-on-lookup-field-in-dynamic-crm-using-script

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans