Tuesday, December 20, 2022

Deactivating Users that are Running Dashboard User

A persistent issue when deactivating users is finding out some time down the line that they were the running user for a dashboard and being presented with the familiar error message:


And yes, I know its possible to run a custom report on dashboards and specify filter criteria to check the running user, but that seems a little clunky and requires too many clicks for my liking.

The next option I considered was a before delete trigger on the User object that would block the deactivation if the user was a dashboard running user.  I've never been keen on these sorts of triggers as I don't think its the greatest experience - you complete all the work and then at the last minute the platform bounces the request.  Plus if a user needs to be deactivated quickly and is the running user for a number of dashboards, a trigger would slow the process down.

What I really wanted was a way to check if there were going to be problems before I deactivate.

The first part to the solution was to create a Visualforce page to display any dashboards that would be affected.  Its a pretty straightforward page to display either a message that the user isn't a dashboard running user, or to list the affected dashboards:?

<apex:page extensions="UserDashboardController" standardcontroller="User">
   <apex:pageblock title="User Dashboard Information">
 <apex:outputpanel rendered="{!NOT(UserNoDashboards)}">
        <apex:outputtext>
           User {!User.username} is the running user for the following dashboards:
 
 
        </apex:outputtext>
  <apex:pageblocktable value="{!userDashboards}" var="db">
   <apex:column headervalue="Dashboard">
      <apex:outputlink target="_blank" value="/{!db.id}">{!db.title}</apex:outputlink>
   </apex:column>
  </apex:pageblocktable>
 </apex:outputpanel>
 <apex:outputpanel rendered="{!userNoDashboards}">
  User {!User.username} is not a running user of any dashboards.
 </apex:outputpanel>
  </apex:pageblock>
</apex:page>

The page is backed by the User standard controller and an extension controller that adds a couple of methods for retrieving dashboard information:?

public class UserDashboardController
{
 User theUser;
  
 public UserDashboardController(ApexPages.StandardController std)
 {
  theUser=(User) std.getRecord();
 }
  
 public List<Dashboard> getUserDashboards()
 {
  List<Dashboard> dbs=[select id, Title from Dashboard
                       where Type='SpecifiedUser'
                         and RunningUserId=:theUser.id];
                   
   
  return dbs;
 }
  
 public Boolean getUserNoDashboards()
 {
  return getUserDashboards().isEmpty();
 }
 
}

Accessing the page with the id of the user I'm considering deactivating shows that they are the running user for one dashboard:


Unfortunately, Visualforce pages can't be embedded into User page layouts, so I need another mechanism to gain access to the Visualforce page. The solution is to create a custom link (Setup -> Customize -> Users -> Custom Links) that opens the Visualforce page in a new window. The configuration for my Custom Link is shown below:



I can then add this custom link to my User page layout and the next time I'm considering deactivating a user, I simply navigate to the user's detail record, click the link and the details appear in a popup window.


It would be quite simple to extend this to output other information pertinent to deactivating a user - the accounts that the user is the owner of for example.

No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...