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

Dynamics 365 (CRM): Java Script filtering on Entity Form

(0) ShareShare
ReportReport
Posted on by

Hello Community,

In Dynamics 365 (CRM) Online, when we use Java Script On Load and On Change on the entity form to filter out lookup field values based on an option set value selection, does anyone know how we can "undo" the filtering if we want to select a different option set value (for example if we make a mistake in the option set value selection, before saving the record)?

For example, if we have the following scenario:

Country Field (Option Set field), containing the values: Canada, US

City Field (lookup field), containing all the Canadian and US cities

The requirements are:

- If we select "Canada" in the Country field, the Java Script would filter out the US cities and only display the Canadian cities in the City field

- Conversely, if we select "US" in the Country field, the Java Script would filter out the Canadian cities and only display the US cities in the City field

The problem we are running into is:

- Initially, we select "Canada" in the Country field, and the filtering works as expected, BUT then when we change the selection to "US", the City field becomes blank and we don't even see the US cities (or even Canadian cities) in the list. So it's as if the filtering stays on and we couldn't undo the filtering. How can we undo/clear this filtering, so that every time we make a change in the option set field dropdown, the Java Script would re-apply the filtering. Please note that we already check that our Java Script function does get called On Change of the option set field on the form.

Example of the Java Scripts we have:

function addLookupCityLookup(filterIds) {

 

    fetchXml = "<filter type='and'><filter type='or'>";

    var partsOfStr = filterIds.split(',');

    for (var i = 0; i < partsOfStr.length; i++) {

        fetchXml += "<condition attribute='new_filterid' operator='eq' value='" + partsOfStr[i] + "' />"

    }

    fetchXml += "</filter></filter>";

    Xrm.Page.getControl("new_citytype").addCustomFilter(fetchXml);

}

 

Any help would be appreciated! Thanks.

I have the same question (0)
  • Verified answer
    Joana Pinto Profile Picture
    740 on at

    Hi,

    I replicated this scenario on my trial environment and it went well for me.

    On the account, if "fax" field was equal to "Joana" then it pre-filtered Parent Account with accounts starting with A. If fax value was "Pinto", then it pre-filtered with accounts starting with B. Here's the code:

    function onLoad(executionContext) {
    
        // get the form context
        var formContext = executionContext.getFormContext();
        formContext.getControl("parentaccountid").addPreSearch(filterAccounts);
    }
    
    function filterAccounts(executionContext) {
        
        var formContext = executionContext.getFormContext();
        var filter = "";
        var fax = formContext.getAttribute("fax").getValue();
    
        if (fax == "Joana") {
            filter  = "";
        } else if(fax == "Pinto") {
            filter  = "";
        }
    
        filter  = "";
    
        formContext.getControl("parentaccountid").addCustomFilter(filter, "account");
    
    }

    I just called my "onLoad" function on onLoad and onChange of fax field events. I am using the latest recommendations of MS:

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

    When you debug, it doesn't throw any error? I am not seeing the full code so I don't know what is before that.

  • Community Member Profile Picture
    on at

    Hi Joana,

    Thank you very much for your inputs and information. While that shows how we can set the filters accordingly, I am curious to know if there is a way we can clear any filters that have been applied. In other words, inside the Java Scripts, once filtered, how do we completely remove the filtering and get all the items back?

    Would you know I could set that?

    Thank you!

  • Verified answer
    Joana Pinto Profile Picture
    740 on at

    Hi,

    Yes, there is.

    formContext.getControl(arg).removePreSearch(yourFunction);

    In my example, it's like this:

    formContext.getControl("parentaccountid").removePreSearch(filterAccounts);

    Please, see the link:

    docs.microsoft.com/.../removepresearch

    Regards,

    Joana

  • Community Member Profile Picture
    on at

    Thanks Joana for the information, that really helps! Much appreciated for your time on this! :)

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 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans