π― 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
π§© 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 usingaddCustomView()
.
π» Example
π Side-by-Side Comparison
Feature | PreSearch | AddCustomView |
---|---|---|
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) |
Complexity | Simple | More involved |
π¦ When to Use What?
Scenario | Use PreSearch | Use 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
Post a Comment