function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PJayaPJaya 

User details in above the hierarchy

Hi All, can tell me how do i get the all the user details above me in the hirerarchy

 

example :

 

1--->>-- 2,3  ---4,5--4--6,5--7-----8--9--10

 

when ever  user 8 act's on account,  i need to get all the users details above him

 

in the above case all users are 1,2 3,4,5,6,7,

 

can any one help m on this .

 

Thanks in advance.

 

 

Yoganand GadekarYoganand Gadekar

Following method will give you all the users above the input users role heirachy, you only need to pass the role id of your desired user. Make sure you add the role id in a set and then pass that set to the following methode. Methode will return you entire list of users above the heirachy.

 

private static List<user> getAllSubRole(set<ID> roleIds) {    
Set<ID> currentRoleIds = new Set<ID>();
Public List <User> UsersList = New List<User>();
   
 // get all of the roles underneath the passed roles   
for(UserRole userRole :[select Id,name from UserRole where ParentRoleId  IN :roleIds AND ParentRoleID != null]){   
currentRoleIds.add(userRole.Id);   
  
}   

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

for(User userRec:[Select Id, Name From User where UserRoleId IN :currentRoleIds And IsActive =: True]){     
UsersList.add(userRec);      

return UsersList;  
}

 

Hit kudos and mark this as answer if it helps you.

 

Thanks and Regards,

Yoganand.

PJayaPJaya

hi yoganand

 

thanks for ur reply

 

when iam tring to excute the code

given by u its giving me the following exception

 

Error: Compile Error: Method does not exist or incorrect signature: getAllSubRoleIds(SET<Id>) at line

if(currentRoleIds.size() > 0)     
     currentRoleIds.addAll(getAllSubRoleIds(currentRoleIds));

 

Here without completion of the method we are trying to call that method again

 

Please help me out on this

 

Thanks in advance,

Yoganand GadekarYoganand Gadekar

You can try with below, below is the methode for returning the roleID's.(this methode works as i have tested it)

 

Public Class UserRoelsInHeirachy{

 Public 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,name from UserRole where ParentRoleId IN :roleIds AND ParentRoleID != null]){
       currentRoleIds.add(userRole.Id);   
       System.debug('$$$User Roles'+userRole.name);   
    }   
  
   // Fetch more roles   
   if(currentRoleIds.size() > 0)     
   currentRoleIds.addAll(getAllSubRoleIds(currentRoleIds));    
      
   return currentRoleIds;  
 }

}

 

 

Trigger that calls above methode,

(add the the role id of required user in the set and pass that set to the above methode)

 

trigger GetUsersInHeirachy on Account (Before Update) {
Public Set<Id> IdSet = New Set<Id>();
id roleID = [select id from Userrole where Developername =:'VPNorthAmericanSales'].id;
IdSet.add(roleID);

Set <ID> rolesIDInHeirachy = UserRoelsInHeirachy.getAllSubRoleIds(IdSet);
List<UserRole> UsersList = [select name from userrole where id In:rolesIDInHeirachy ];

for(UserRole us:UsersList )
System.debug('**-- UserRoleNames--**'+us.name);

}

 

Hit Kudos and mark this as answer if it helps you.