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)

JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

(0) ShareShare
ReportReport
Posted on by 85

Hi all

I have a custom field that has the customer type. On a specific form, I want to filter this lookup to include only accounts and contacts that have a specific value in a custom field that has the same name on both entities. I have written the following JavaScript to accomplish this:

function OnLoad(executionContextObj) {
         debugger;

         var formContext = executionContextObj.getFormContext();
         formContext.getControl("pit_recipient").addPreSearch(ApplyRecipientsOnlyFilter);
};

function ApplyRecipientsOnlyFilter(executionContextObj) {

         debugger;

               var filter = "<filter type='and'>" +
                                       "<condition attribute='pit_isarecipient' operator='eq' value='1' />" +
                                 "</filter>";

                var formContext = executionContextObj.getFormContext();
                formContext.getControl("pit_recipient").addCustomFilter(filter);
          };

 

On the standard Web client, this works exactly as expected.

But in the custom app we have created for our users, the ApplyRecipientsOnlyFilter() method is not even called and the accounts and contacts are not filtered as expected.

Can anyone help me fix this issue please?

Thanks

Ashley Visagie

*This post is locked for comments

I have the same question (0)
  • ARIFNIIT Profile Picture
    1,391 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

    What do you mean by  custom app? Are you using USD?

  • Shahbaaz Ansari Profile Picture
    6,211 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

    Hi Ashley,

    Please try with below code,

    function OnLoad(executionContextObj) {

            var formContext = executionContextObj.getFormContext();

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

    var filter = "<filter type='and'>" +

                                          "<condition attribute='pit_isarecipient' operator='eq' value='1' />" +

                                    "</filter>";

                   var formContext = executionContextObj.getFormContext();

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

    });

    }

    Best Regards,

    Shahbaaz.

  • gdas Profile Picture
    50,091 Moderator on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

    Hi Ashley,

    You have to pass executioncontextObj in the function ApplyRecipientsOnlyFilter(). 

            function OnLoad(executionContextObj) {
                debugger;
    
                var formContext = executionContextObj.getFormContext();
                if (formContext.getControl("pit_recipient") != null) {
                    Xrm.Page.getControl(CountryFieldName).addPreSearch(
                    function () {
                        ApplyRecipientsOnlyFilter(executionContextObj);
                    });
                }
            };
    
            function ApplyRecipientsOnlyFilter(executionContextObj) { 
                var filter = "<filter type='and'>" +
                                        "<condition attribute='pit_isarecipient' operator='eq' value='1' />" +
                                  "</filter>";
                var formContext = executionContextObj.getFormContext();
                formContext.getControl("pit_recipient").addCustomFilter(filter);
            };
    


    Hope this helps.

  • Ashley Visagie Profile Picture
    85 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

    Not USD, just created an App that uses the Unified Interface

  • Ashley Visagie Profile Picture
    85 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

    Hi Goutam

    Thanks for your reply.

    This is basically the same code (give or take) that I posted because when you register a handler via addPreSearch(), the execution context is passed to this handler automatically.

    However, I tried your version and it doesn't work either.

    The inline function you specify in the addPreSearch() call is not called - I verified this by adding a debugger statement and debugging it using F12 in Chrome.

    My original code works as expected when I use the normal CRM web client; it doesn't work in the custom Unified Interface app.

    Regards

    Ashley

  • Ashley Visagie Profile Picture
    85 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

    Hi Shahbaaz

    Thanks for your reply.

    I tried the code you posted but it doesn't work either.

    The inline function you specify in the addPreSearch() call is not called - I verified this by adding a debugger statement and debugging it using F12 in Chrome.

    My original code works as expected when I use the normal CRM web client; it doesn't work in the custom Unified Interface app.

    Regards

    Ashley

  • ARIFNIIT Profile Picture
    1,391 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

    you mean mobile App?

  • Daniel Wikell Profile Picture
    2,360 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

    I'm guessing you created a D365 app (the one where you get to specify sitemap, which forms and views are available etc.).

    I went and checked this out on an environment where I have the same kind of setup. A presearch added to a customer lookup. The presearch works fine for me both from the default app and from a custom app created through the app designer.

    Could it be that you are exposing a different form in your app and in this form you don't have the script libraries loaded?

  • Ashley Visagie Profile Picture
    85 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

    Hi Daniel

    Thanks for your response.

    I can confirm that it is the correct form (because it is a custom one we've created).

    I also can confirm that the script libraries are loaded because I can debug the code.

    However, the breakpoint in the function, ApplyRecipientsOnlyFilter, is never called.

    With the app you created, do you choose "Web" or "Unified Interface" client?

    My issue is with an app of the latter kind.

    Regards

    Ashley

  • Sumedha Obeysekera Profile Picture
    307 on at
    RE: JavaScript addPreSearch() filter on customer lookup not working in v9 Unified Client app

    I suggest implementing two separate filters for Account and Contact as this; sumedha8.blogspot.com.au/.../filtered-lookup-for-customer-type-fields.html

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans