Tag Archives: Salesforce

Get Number of Active Users In a Permission Set Using SOQL Query

SQOL Query:

SELECT COUNT(AssigneeId) FROM PermissionSetAssignment WHERE Assignee.isActive = true AND PermissionSet.Name = 'My_Sample_PermissionSet'

Note: Change the Permission Set API Name as per your requirement.

Get All Salesforce Users in Role Hierarchy Using Apex

Controller:

Public with sharing class Sample{
    Public List<string> CusrrentUserRole{get;set;}
    Public List<String> RolesInHierarchy{get;set;}
    Public Sample(){
        CusrrentUserRole = New List<string>();
        CusrrentUserRole.add([SELECT UserRoleId FROM User Where Id =:UserInfo.getUserId() LIMIT 1].UserRoleId);
    }
    Public Void getRollInHierarchy(){
        
        RolesInHierarchy = New List<Id>();
        RolesInHierarchy = getAllRoleInHierarchy(CusrrentUserRole);
    }
    
    Public List<string> getAllRoleInHierarchy (List<Id> UserRole) {     
        List<string> currentRoleIds = new List<string>();     
        for(UserRole userRoleName :[SELECT Id,name FROM UserRole Where ParentRoleId  = :UserRole AND ParentRoleID != null]){    
            currentRoleIds.add(userRoleName.id);    
        }    
        if(currentRoleIds.size() > 0) {     
            currentRoleIds.addAll(getAllRoleInHierarchy(currentRoleIds)); 
        }    
        List<String> RoleList = New List<String>();  
        for(UserRole r:[SELECT Name From UserRole Where Id In:currentRoleIds]){
            RoleList.add(r.name);      
        }
        return RoleList;
    }        
}

Visualforce Page:

<apex:page controller="Sample">
 <apex:form>
   <apex:pageBlock>
     <apex:commandButton value="Show Roles below my Hierarchy" action="{!getRollInHierarchy}"/> 
   </apex:pageBlock>
   <apex:pageBlock >
   <b> Role below in Hierarchy are : </b>{!RolesInHierarchy}
   </apex:pageBlock>
  </apex:form>
</apex:page>

Output: