adds filters to the results displayed in the lookup --
Customize result of lookup in Dynamics 365 CRM/CE using addCustomFilter
In Microsoft Dynamics 365, we can use addCustomFilter to modify the results displayed in a lookup field using Javascript. i.e.
- using Filterxml condition
- add addPreSearch
- addCustomFilter
Scenario
- We have a requirement, that the account lookup field on contact should only show the accounts which contains phone number.
- On load of the contact form, our JavaScript will trigger, and the filter will be added to the Account lookup field.
- JavaScript (Event Handler) will be added on the Contact entity. and On Load Event of the contact form. So, the JavaScript function will trigger on load of the contact form.
Syntax
- Below is the latest syntax used for addCustomFilter.
formContext.getControl(arg).addCustomFilter(filter, entityLogicalName)
Parameters
-
- filter: String. The fetchXml filter element to apply.
- entityLogicalName: (Optional) String. If this is set, the filter only applies to that table type. Otherwise, it applies to all types of tables returned.
addCustomFilter useCase Example
Below is the JavaScript function “changeAccountLookupFilter”
- 1st create form context from execution context passed as 1st parameter
- In a variable store our query.
*Note-Query is generated using advanced find.
- Created a function and use addCustomFilter inside it.
- Call this function on addPreSeach of the account lookup field on contact.
function changeAccountLookupFilter(executionContext) { debugger; var formContext = executionContext.getFormContext(); formContext.getControl("parentcustomerid").addPreSearch(filterCustomerAccounts); } filterCustomerAccounts = function(executionContext) { var formContext = executionContext.getFormContext(); var customerAccountFilter = "<filter type='and'><condition attribute='telephone1'operator='not-null'/></filter>"; formContext.getControl("parentcustomerid").addCustomFilter(customerAccountFilter, "account"); }
We have added the Javascript on Load of contact form.