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.