In most cases, you can filter lookup columns by setting a view directly in the column properties of a form in Dynamics 365. However, this option may not be available for certain lookup columns, such as the OwnerId
field. In these cases, we need to use JavaScript to apply the filter and dynamically set the view. This article will walk you through three key points: filtering the OwnerId
lookup to show only users, applying a view to the lookup, and dynamically retrieving the view ID by its name.
1. Filtering the OwnerId
Column to Show Only Users
To ensure that the OwnerId
lookup only displays users, we use the setEntityTypes()
method. This method filters the entities displayed in the lookup. For example, to restrict the OwnerId
lookup to show only users (systemuser
entity):
If you want to display only teams, you can modify the setEntityTypes()
method to filter for teams:
This allows flexibility depending on whether you want to display users or teams in the lookup.
2. Applying a View to the Lookup Column
Sometimes, you might want to apply a specific view to further filter the lookup results based on predefined criteria. You can use the setDefaultView()
method to set a specific view on the OwnerId
lookup field.
This method requires the viewId
(GUID) of the view you want to set. It ensures that only records matching the selected view are displayed when the user interacts with the lookup.
3. Retrieving the View ID by Name
Instead of hardcoding the viewId
, you can dynamically retrieve the view ID by its name using the Xrm.WebApi.retrieveMultipleRecords()
method. This is especially useful when you don’t want to hardcode GUIDs in your scripts. The following code demonstrates how to retrieve the view ID by its name and apply it to the OwnerId
lookup:
Putting It All Together
This approach allows you to filter the OwnerId
lookup to display only users or teams, apply a specific view to the lookup, and dynamically retrieve the view ID by its name. This way, you can ensure that your lookup behaves as expected without the need for hardcoding view GUIDs, making your solution more flexible and maintainable.
No comments:
Post a Comment