How to Filter the OwnerId Lookup to Show Only Users and Dynamically Set a View by Name in Dynamics 365

 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):

lookupControl.setEntityTypes(["systemuser"]);

If you want to display only teams, you can modify the setEntityTypes() method to filter for teams:

lookupControl.setEntityTypes(["team"]);

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.

lookupControl.setDefaultView(viewId);

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:


function setViewByNameUsingRetrieveMultiple(executionContext, viewName)
{
    var formContext = executionContext.getFormContext();
    var lookupControl = formContext.getControl("ownerid"); // Control name for OwnerId
    if (lookupControl == null)
    {
        console.error("OwnerId lookup control not found.");
        return;
    }

    // Query to retrieve the savedquery (system view) by name
    var query = "?$select=savedqueryid&$filter=name eq '" + viewName + "'";

    // Use Xrm.WebApi.retrieveMultipleRecords to fetch the view by name
    Xrm.WebApi.retrieveMultipleRecords("savedquery", query).then(
        function success(result)
        {
            if (result.entities.length > 0)
            {
                var viewId = result.entities[0].savedqueryid; // Get the view ID
                lookupControl.setEntityTypes(["systemuser"]); // Set to show only Users
                lookupControl.setDefaultView(viewId); // Set the dynamically retrieved view ID
                console.log("View ID set to: " + viewId);
            }
            else
            {
                console.error("No view found with the name: " + viewName);
            }
        },
        function(error)
        {
            console.error("Error retrieving views: " + error.message);
        }
    );
}

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