Get All Sub-ordinate User in Role Hierarchy

Que: To Get all Sub-ordinate users in role hierarchy .

Solution :  Create a Util Class:


public with sharing class RoleUtils {

  public static Set<ID> getRoleSubordinateUsers(Id userId) {

    // get requested user's role
    Id roleId = [select UserRoleId from User where Id = :userId].UserRoleId;
    // get all of the roles underneath the user
    Set<Id> allSubRoleIds = getAllSubRoleIds(new Set<ID>{roleId});
    // get all of the ids for the users in those roles
    Map<Id,User> users = new Map<Id, User>([Select Id, Name From User where 
      UserRoleId IN :allSubRoleIds]);
    // return the ids as a set so you can do what you want with them
    return users.keySet();

  }

  private static Set<ID> getAllSubRoleIds(Set<ID> roleIds) {

    Set<ID> currentRoleIds = new Set<ID>();

    // get all of the roles underneath the passed roles
    for(UserRole userRole :[select Id from UserRole where ParentRoleId 
      IN :roleIds AND ParentRoleID != null])
    currentRoleIds.add(userRole.Id);

    // go fetch some more rolls!
    if(currentRoleIds.size() > 0)
      currentRoleIds.addAll(getAllSubRoleIds(currentRoleIds));

    return currentRoleIds;

  }

}



// Create a New Main Class

Public Class Abc
{
public list<SelectOption> options {get;set;}
 public string UserId{get;set;}

  public void showUserList()

{
 options = new list<SelectOption>();

   set<Id> setSubordinateUserIds = RoleUtils.getRoleSubordinateUsers(Userinfo.getUserId());
      system.debug('ERTG'+setSubordinateUserIds );
      for(User  u : [select id, name from user where isActive= true and Id != :Userinfo.getUserId() and Id IN: setSubordinateUserIds  order by name limit 999])
      {
         options.add(new SelectOption(u.Id,u.Name));              
      }
    
      
                                                                                                 
    }
}

}

--------------------------------------VF Page Code-------------------------------------------------

<apex:page>


<apex:form id="frmid">

  <apex:pageMessages id="msgid"></apex:pageMessages>

    <apex:pageBlock >
       <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
             Sub-Ordinate User:
             <apex:selectList multiselect="false" size="1" value="{!UserId}" id="UserVal">     
                 <apex:selectOptions value="{!options}"/>
             </apex:selectList>
        </apex:pageBlockSectionItem>
        
      
       </apex:pageBlockSection>
       <apex:pageBlockButtons location="bottom">
          <apex:commandButton value="Save" action="{!SaveRecord}"/>
          <apex:commandButton value="Cancel" action="{!ReturnToPage}" />
       </apex:pageBlockButtons>
    </apex:pageBlock>
  
  </apex:form>
  

</apex:page>



  Note: In case of any issue, please feel free ask to me 
   Thanks
Sumit Shukla
Sumitshukla.mca@gmail.com
CCCINFOTECH.COM





Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page