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.

  • Community Member Profile Picture
    on at
    RE: Dynamics 365 (CRM): Java Script filtering on Entity Form

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

  • Verified answer
    Joana Pinto Profile Picture
    740 on at
    RE: Dynamics 365 (CRM): Java Script filtering on Entity Form

    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
    RE: Dynamics 365 (CRM): Java Script filtering on Entity Form

    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
    RE: Dynamics 365 (CRM): Java Script filtering on Entity Form

    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.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

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

#1
Siv Sagar Profile Picture

Siv Sagar 149 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 57

#3
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 53 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans