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
Ramakrishnan AyyanarRamakrishnan Ayyanar 

Get Same Role and Subordinate Users

This methods are used for Get Same Role and Subordinate Users.

public static Set<ID> getSameRoleSubordinateUsers()
     {
         Set<ID> userIds = new Set<ID>();
     
        // get current user's role id
          Map<Id,User> users = new Map<Id, User>([Select Id, Name From User where UserRoleId=:userinfo.getuserroleid()]);
          userIds.addAll(users.keySet());
         
        // get all of the roles underneath the user
         Set<Id> allSubRoleIds = getAllSubRoleIds(new Set<ID>{userinfo.getuserroleid()});
        
        // get all of the ids for the users in those roles
         Map<Id,User> subUsers = new Map<Id, User>([Select Id, Name From User where
          UserRoleId IN :allSubRoleIds]);
         
          userIds.addAll(Subusers.keySet());
      
        // return the ids as a set so you can do what you want with them
         return userIds;
    }

   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;

    }

Thank's 
Ramakrishnan Ayyanar
+919944112175
Vinit_KumarVinit_Kumar
What's the issue you are facing with this code ??
SFDC_DevloperSFDC_Devloper
Hi Ramakrishnan,

try below code.....
public static set<Id> getSubordinateRoles(Id roleId) {
    map<Id, set<Id>> parentAndChildren = new map<Id, set<Id>>();
    set<Id> children;
    for(UserRole ur : [select Id, ParentRoleId from UserRole]) {
        children = parentAndChildren.containsKey(ur.ParentRoleId) ? parentAndChildren.get(ur.ParentRoleId) : new set<Id>();
        children.add(ur.Id);
        parentAndChildren.put(ur.ParentRoleId, children);
    }
    return getSubordinateRoles(role, parentAndChildren);
}

public static set<Id> getSubordinateRoles(Id roleId, map<Id, set<Id>> parentAndChildren) {
    set<Id> subordinateRoles = new set<Id>();
    set<Id> remainingSubordinateRoles = new set<Id>();
    if(parentAndChildren.containsKey(roleId)) {
        subordinateRoles.addAll(parentAndChildren.get(roleId));
        for(Id subRoleId : subordinateRoles) {
            remainingSubordinateRoles.addAll(getSubordinateRoles(subRoleId, parentAndChildren));
        }
    }
    subordinateRoles.addAll(remainingSubordinateRoles);
    return subordinateRoles;
}

Users subordinates:https://developer.salesforce.com/forums/ForumsMain?id=906F00000008yq5IAA

Thanks,
Rockzz
Ramakrishnan AyyanarRamakrishnan Ayyanar
Vinit_Kumar@

It's correctly work.

You just view the code.How to simplify this.