Posts

Filter a Lookup field in Dynamics 365

Shambhu Tiwary

 🚀 Dynamics 365 Tip – Filtering Lookup Fields Made Easy!

Ever needed to filter a Lookup field in Dynamics 365 so users see only relevant records?

Here's how you can do it 👇
🛠 Scenario:
You want the Contact lookup on a custom form to show only Contacts related to a specific Account, Role, or Status.
✅ Solution:
Use JavaScript and addPreSearch() on the form to apply a custom filter dynamically.

💡 You can also dynamically pass GUIDs, roles, or custom attributes depending on 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.