Skip to main content
Sudhir Narayanaswamy ha preguntado en #Apex
Hi, 

  I need to get all the user role and sub roles within sub role that are associated to a parent role using soql query. Below is the code which i am using to get role and sub-roles data. 

 /* Here I pass user name get the userrole id */

 user gurs = [Select UserRoleId,Email,ForecastEnabled 

                     from user where id = :runningUserId LIMIT 1];

/*  Here I pass the userrole id to see parentroleid */

 List<User> UsrList = [Select FirstName, Id, LastName, UserRoleId, UserRole.Name 

                            from User 

                            Where UserRole.ParentRoleId = :gurs.UserRoleId];

I am able to see below roles when i run the above soql code. 

 Example : 

     EMEA - Level - 1

          EMEA- Sub Level -2 

My new requirement is 

 Example 

     EMEA - Level - 1

          EMEA- Sub Level -2 

              EMEA - Super Sub Level-3

If I pass user name I should be able to see all the 3 level hierarchy data if there are ever 4 I should be able to see all the 4 level 

Please suggest me apex experts how i can achive this requirement need you suggestion 

Thanks

Sudhir
8 respuestas
  1. 23 mar 2016, 7:35
    Mahesh,

    Copy paste the below code in Dev Console and Execute

    it. 

    ​

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

    for(UserRole userRole :[select Id from UserRole where ParentRoleId IN :roleIds AND ParentRoleID != null]) { currentRoleIds.add(userRole.Id); }

    if(currentRoleIds.size() > 0)

    { currentRoleIds.addAll(getAllSubRoleIds(currentRoleIds)); }

    system.debug('ROLE ID's'+currentRoleIds);

    ​Please mark this Answered, if this solves your Query.
0/9000