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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

removePreSearch Lookup

(0) ShareShare
ReportReport
Posted on by 1,848

Hi All, 

I'm trying to add PreSearch on lookup field incase of an option in optionset field and trying to remove it on another option. Add is working fine but removePreSearch isn't

onChangeProductType: function (executionContext) {

var formContext = executionContext.getFormContext();

if (formContext.getAttribute("ab_producttype").getValue() == 108730002) {
formContext.getControl("ab_product").addPreSearch(function () {
filterlookup(executionContext);
});
}
else {

formContext.getControl("ab_product").removePreSearch(function () {
filterlookup(executionContext);
});


}
},

filterlookup: function (executionContext) {

var formContext = executionContext.getFormContext();

var filter = "<filter type='and'><condition attribute = 'name' operator = 'like' value = '%Builders%' /><condition attribute='name' operator='like' value='%Single%' /><condition attribute='name' operator='like' value='%60%' /><condition attribute='name' operator='like' value='%Exterior%' /></filter>";

formContext.getControl("ab_product").addCustomFilter(filter);

},

*This post is locked for comments

I have the same question (0)
  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    Please check if you have register the your method onChangeProductType on load as well as on change of product type. If still not working try below code (option 1), if this doesn't work then you can also achieve your requirement by option 2 code.

    // Option 1

    //==========================================================================

    onChangeProductType: function (executionContext) {

       var formContext = executionContext.getFormContext();

       if (formContext.getAttribute("ab_producttype").getValue() == 108730002) {

           formContext.getControl("ab_product").addPreSearch(filterlookup);

       }

       else {

           formContext.getControl("ab_product").removePreSearch(filterlookup);

       }

    },

    filterlookup: function () {    

       var filter = "<filter type='and'><condition attribute = 'name' operator = 'like' value = '%Builders%' /><condition attribute='name' operator='like' value='%Single%' /><condition attribute='name' operator='like' value='%60%' /><condition attribute='name' operator='like' value='%Exterior%' /></filter>";

       formContext.getControl("ab_product").addCustomFilter(filter);

    },

    //==============================================================================

    // Option 2

    //==========================================================================

    onChangeProductType: function (executionContext) {

       var formContext = executionContext.getFormContext();

       if (formContext.getAttribute("ab_producttype").getValue() == 108730002) {

           formContext.getControl("ab_product").addPreSearch(function () {

               filterlookup(executionContext);

           });

       }

       else {

           formContext.getControl("ab_product").removePreSearch(function () {

               filterlookup(executionContext);

           });

       }

    },

    filterlookup: function (executionContext) {

       var formContext = executionContext.getFormContext();

       if (formContext.getAttribute("ab_producttype").getValue() == 108730002) {

           var filter = "<filter type='and'><condition attribute = 'name' operator = 'like' value = '%Builders%' /><condition attribute='name' operator='like' value='%Single%' /><condition attribute='name' operator='like' value='%60%' /><condition attribute='name' operator='like' value='%Exterior%' /></filter>";

           formContext.getControl("ab_product").addCustomFilter(filter);

       }

       else {

           var filterAllProducts = "<filter type='and'><condition attribute = 'productid' operator='not-null'/></filter>";

           formContext.getControl("ab_product").addCustomFilter(filterAllProducts);

       }

    },

    //====================================

    Hope this 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

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
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans