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 365 | Integration, Dataverse...
Suggested Answer

removePreSearch(preSearchHandler) doesnt work. code inside

(0) ShareShare
ReportReport
Posted on by 219

Hello,

I have 2 lookups. On the first one is a javascript which sets a presearchfilter for the second one. When Value "Printpodukte" or in english printproducts is picked the presearchfilter gets set.

however when go back to the first lookup and pick anything else, the filter wont get removed. Below is the code. I already searched in the forums and tried the advices but nothing worked.

I hope anyone finds my failure



function filterLookup() {
    try {
        if (Xrm.Page.getControl("xxx_productfamily") != null && Xrm.Page.getControl("xxx_productfamily") != undefined) {

            if (Xrm.Page.getAttribute('xxx_productfamily').getValue()!=null) {
                var product = Xrm.Page.getAttribute('xxx_productfamily').getValue()[0].name;

                if (product == "Printprodukte")
                {

                    Xrm.Page.getControl("productid").addPreSearch(preSearchHandler);
                }
                else
                {
                    alert('remove');
                    Xrm.Page.getControl("productid").removePreSearch(preSearchHandler);
                }
            }
        }
    } catch (e) {

        throw new Error(e.Message);
    }
}

function preSearchHandler() {
    addCustomeLookupFilter();
}

function addCustomeLookupFilter() {
    try {
      
            fetchxml = "<filter type='and'><condition attribute = 'productstructure' operator = 'eq' value = '3' /></filter >";
            Xrm.Page.getControl("productid").addCustomFilter(fetchxml);
        
    }
    catch (e) {
        throw new Error(e.Message);
    }
}

  • Suggested answer
    Wahaj Rashid Profile Picture
    11,323 on at

    Hi,

    In this scenario, I suggest using Related Record Filtering feature available on the lookup fields.

    Read more about it here:

    carldesouza.com/.../

    However, if you need to use script to do this, you should use Execution Context in the current D365 version (the old method is deprecated) to get form controls and values, example:

    var formContext= executionContext.getFormContext();
    formContext.getControl("productid").addPreSearch(preSearchHandler);

    *dont't forget to pass execution context while registering Java Script function on form.

    You can see this example here for better understanding (this is similar scenario):

    https://www.inogic.com/blog/2019/10/tip-to-filter-customer-field-depending-on-type-of-customer-in-dynamics-365-uci/

    Best,

    Wahaj

    (if it helps, mark it verified)

  • McDauly Profile Picture
    219 on at

    Hi Wahaj,

    I already use the related record filter too, but for a specific value I only need to show bundles instead of products...

    I may also need to mention that we are on the latest d365 on premise version.

  • Wahaj Rashid Profile Picture
    11,323 on at

    Hi,

    As far as I know, there is no supported way of doing this.

    However, you can achieve this on the related entity form (e.g. Opportunity Product).

    Instead of using subgrid to add products, go to related tab, and open the New form (instead of in-line editing).

    I undestrand, subgrid is easier for end-user, but i do not see any other option.

    Thanks,

    Wahaj

  • McDauly Profile Picture
    219 on at

    it already works

    pastedimage1595825781065v1.png

    I have 3 fields. the first defines the which category (family hirarchy 1) is used then in the second the brand (family hirarchy2) is picked. this is archived by a related filter..

    then in the choosen product is a related filter and the by setting the category (hirarchy1) also a prefilter is set..
    pastedimage1595826425414v2.png
    (for privacy i removed some brands in the picture)

    And that works. it shows only the bundles...

    However, the problem is that the removel of the prefilter doesnt work. It's only for the usecase that the user picks the wrong category in the first place and wants to correct himself. He first picks printproducts and the filter is set. he realises he made a mistake and changes the digital products and the filter doesnt remove with this code:

    var Sdk = window.Sdk || {};
    var globalcontext;

    Sdk.filterLookup = function (executionContext) {


        var formContext = executionContext.getFormContext();
        globalcontext = executionContext;

        try {
            if (formContext.getControl("xxx_productfamily") != null && formContext.getControl("xxx_productfamily") != undefined) {

                if (formContext.getAttribute('xxx_productfamily').getValue() != null) {
                    var product = formContext.getAttribute('xxx_productfamily').getValue()[0].name;

                    if (product == "Printprodukte")
                    {

                     
                        formContext.getControl("productid").addPreSearch(Sdk.addCustomeLookupFilter);
                    }
                    if (product == "Digitalprodukte")
                    { 
                        formContext.getControl("productid").removePreSearch(Sdk.addCustomeLookupFilter);

                    }


                   
                }
            }
        } catch (e) {

            console.log(e);
            throw new Error(e.Message);

        }
    }

    Sdk.addCustomeLookupFilter = function () {
     
        
                try {

                    var formContext = globalcontext.getFormContext();
                    fetchxml = "<filter type='and'><condition attribute = 'productstructure' operator = 'eq' value = '3' /></filter >";
                    formContext.getControl("productid").addCustomFilter(fetchxml, "product");

                }
                catch (e) {
                    console.log(e);
                    throw new Error(e.Message);
                }
         
        }

    I dont understand it and it drives me crazy. also the documentation of removePreSearch isnt any helpfull

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/controls/removepresearch

    I searched through the internet and tried every approach but nothing removed the filter...

    But why should it be not possible if the function exists?

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
11manish Profile Picture

11manish 73 Super User 2026 Season 2

#2
ParthPatel249 Profile Picture

ParthPatel249 56

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 44 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans