🎯 Dynamics 365: Difference Between PreSearch and AddCustomView in Lookup Fields

 

In Microsoft Dynamics 365, lookups are essential for establishing relationships between entities. But sometimes, you don’t want users to see every possible record in a lookup. Instead, you may want to filter the results or even show a completely custom view. That’s where two powerful client-side APIs come into play: PreSearch and AddCustomView.

Although they can appear similar, they serve different purposes. In this blog, we’ll break down their differences, use cases, and code examples to help you decide which one to use—and when.


πŸ” What is PreSearch?

The PreSearch event lets you dynamically filter the records shown in an existing lookup view right before the user opens the lookup. It's useful when your filtering criteria depend on other field values on the form.

✅ Use Case

You want to filter a Contact lookup to only show Active Contacts or only those related to the selected Account.

πŸ“¦ How It Works

You hook into the lookup's addPreSearch method, and within that callback, you apply a filter using addCustomFilter().

πŸ’» Example


formContext.getControl("contactid").addPreSearch(function () { var filter = "<filter type='and'>" + "<condition attribute='statecode' operator='eq' value='0' />" + "</filter>"; formContext.getControl("contactid").addCustomFilter(filter); });

🧩 What is AddCustomView?

AddCustomView allows you to define and inject an entirely new lookup view using custom FetchXML and LayoutXML. This gives you full control over what data to fetch and how it should be displayed.

✅ Use Case

You want to show a list of special "VIP Contacts" in the Contact lookup with specific columns, sorted by Last Name.

πŸ“¦ How It Works

You define:

  • a custom view ID (GUID),

  • the entity logical name,

  • a display name,

  • your own FetchXML query,

  • a LayoutXML (for columns),
    and then add it using addCustomView().

πŸ’» Example


var viewId = "00000000-0000-0000-0000-000000000001"; var entityName = "contact"; var viewDisplayName = "VIP Contacts"; var fetchXml = "<fetch>...</fetch>"; // your custom query var layoutXml = "<grid name='resultset' ...>...</grid>"; // your layout formContext.getControl("contactid").addCustomView( viewId, entityName, viewDisplayName, fetchXml, layoutXml, true // make it the default view );

πŸ“Š Side-by-Side Comparison

FeaturePreSearchAddCustomView
Filters records?✅ (existing views only)✅ (via custom FetchXML)
Creates a new view?
Custom columns?
Default view option?
Based on form data?✅ (if FetchXML is dynamic)
ComplexitySimpleMore involved


🚦 When to Use What?

ScenarioUse PreSearchUse AddCustomView
Filter existing system view
Show entirely new data set
Need custom layout/columns
Depend on other form fields✅ (with dynamic FetchXML)


πŸ’‘ Pro Tip: Combine Both

Yes, you can use both together. For example, use PreSearch to filter the default lookup view and AddCustomView to offer an additional option with custom logic.


 Conclusion

Both PreSearch and AddCustomView are indispensable tools for customizing lookup behavior in Dynamics 365. Use PreSearch when you want to filter existing lookup views based on form data. Use AddCustomView when you need full control over what data appears and how it's shown.

By choosing the right tool for the job, you can deliver a better, more streamlined experience to your users.

Comments

Popular posts from this blog

πŸ€– Copilot vs Microsoft Copilot vs Copilot Studio: What’s the Difference?

Understanding Auto-Numbering in a Multi-Transaction System

Integrating Dynamics 365 CRM with MuleSoft Using a Synchronous C# Plugin