
I have a Model-Driven App with a Timesheet Entry form I am developing for a law firm that contains lookup fields for Client and Matter.
The Client and Matter tables are related: the Matter table (cr4cc_xmatter) has a lookup to Client (cr4cc_client).
Each Matter has a status field (cr4cc_currentstatus), where 1 = Active and 0 = Passive
I want the Client lookup field on the Timesheet Entry form to show only those Clients who have at least one related Matter where cr4cc_currentstatus = 1 (i.e., only Clients with at least one Active Matter).
I tried adding a custom JavaScript function to the Timesheet Entry form to filter the Client lookup dynamically.I registered this function on the form’s OnLoad event and on the Client field’s PreSearch event. I highlighted "Pass execution context as first parameter" checkbox.
Here’s the code I used:
function filterClientLookup(executionContext) {
var formContext = executionContext.getFormContext();
formContext.getControl("cr4cc_client").addPreSearch(function () {
var fetchXml =
"<filter type='exists'>" +
"<entity name='cr4cc_xmatter'>" +
"<filter type='and'>" +
"<condition attribute='cr4cc_client' operator='eq' uitype='cr4cc_xclient' value='{GUID}' />" +
"<condition attribute='cr4cc_currentstatus' operator='eq' value='1' />" +
"</filter>" +
"</entity>" +
"</filter>";
formContext.getControl("cr4cc_client").addCustomFilter(fetchXml);
});
}
Can anybody help me how I can create a cascading drop down with this kind of filtering ?
Has anyone successfully implemented a lookup filter based on the existence of related records in a Model-Driven App using JavaScript?
Is my FetchXML filter incorrect for this scenario? Do I need to pass a dynamic GUID, or use a different FetchXML structure for the addCustomFilter method?
Is there a better supported way to accomplish this (e.g., plugin, Power Automate, calculated field)?
Error Log