Posts

Filter a Lookup field in Dynamics 365

Shambhu Tiwary

Power Platform Tip: Filtering Lookup Fields Made Easy

Have you ever needed to filter a Lookup field in Power Platform to ensure users see only the most relevant records? Here is a straightforward method to achieve this.

The Scenario

Consider a situation where you need the Contact lookup on a custom form to display only Contacts related to a specific Account, Role, or Status.

The Solution

You can apply a custom filter dynamically by utilizing JavaScript and the addPreSearch() method directly on your form.

Note: You can also dynamically pass GUIDs, roles, or custom attributes to adapt to your specific business logic.
function filterContacts(executionContext) {
    var formContext = executionContext.getFormContext();

    formContext.getControl("your_lookup_field").addPreSearch(function() {
        formContext.getControl("your_lookup_field").addCustomFilter(
            `<filter type='and'>
                <condition attribute='statuscode' operator='eq' value='1' />
            </filter>`,
            "contact"
        );
    });
}
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.