Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
Suggested answer

removePreSearch(preSearchHandler) doesnt work. code inside

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

  • McDauly Profile Picture
    McDauly 219 on at
    RE: removePreSearch(preSearchHandler) doesnt work. code inside

    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?

  • Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: removePreSearch(preSearchHandler) doesnt work. code inside

    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
    McDauly 219 on at
    RE: removePreSearch(preSearchHandler) doesnt work. code inside

    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.

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: removePreSearch(preSearchHandler) doesnt work. code inside

    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)

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,252 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,089 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans