web
You’re offline. This is a read only version of the page.
close
Skip to main content
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
    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

  • AnkitSinghal Profile Picture
    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.

  • mvilas02 Profile Picture
    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
    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.

  • Aric Levin - MVP Profile Picture
    30,190 Moderator 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);

    }

  • Suggested answer
    ashlega Profile Picture
    34,477 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);
    });
    }
    }

  • Gokul kansagara Profile Picture
    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);

     }

    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Wayne Walton Profile Picture

Wayne Walton 2

#2
Good.Panos Profile Picture

Good.Panos 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans