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
Taresh PandeyTaresh Pandey 

How to write Apex class for Role Hierarchies without using Visual Force Page ?

Hello All,

I have a small question that; is it possible - creation of apex class for Role Hierarchy without using VF pages. If answer is YES then tell me How ?


thank you all experts in advance.

Regards'
Taresh Pandey
Best Answer chosen by Taresh Pandey
Ajay K DubediAjay K Dubedi
Hi Taresh Pandey,

You can just go through this link which I think fulfill  the requirement:-http://http://blog.jeffdouglas.com/2011/02/15/find-my-salesforce-users-by-role-hierarchy/

Thanks.

All Answers

Ajay K DubediAjay K Dubedi
Hi Taresh Pandey,

You can just go through this link which I think fulfill  the requirement:-http://http://blog.jeffdouglas.com/2011/02/15/find-my-salesforce-users-by-role-hierarchy/

Thanks.
This was selected as the best answer
Taresh PandeyTaresh Pandey
Thank you Mr. Ajay 
nishant verma11nishant verma11
Hi Taresh,

I think this code helps you for your question  


public class RoleUser
{
  
  public class InnerRoleUser
  {
    public string Name;
    public string Id;
    public list<InnerRoleUser> SRL;
  }
    public list<InnerRoleUser> mainparent ()
    {
            list<UserRole> RL= [SELECT Id,Name,ParentRoleId  FROM UserRole  ORDER BY ParentRoleId];
            list<InnerRoleUser> URL = new list<InnerRoleUser>();
     
      for(UserRole RLR :RL)
      {
          InnerRoleUser URN = new InnerRoleUser();
        if(RLR.ParentRoleId==null)
        { 
          URN.Name = RLR.Name;
          URN.Id = RLR.Id;
          URN.SRL =childmethod(URN.Id,RL);
          URL.add(URN);
        }
      }  return URL;
    }
    public list<InnerRoleUser> childmethod (string sID , list<UserRole> LR )
    {
      list<InnerRoleUser> templist= new list<InnerRoleUser>();
      for(UserRole INR: LR)
      {
        if(sId == INR.ParentRoleId)
        {
        InnerRoleUser tl = new InnerRoleUser();
        tl.Name= INR.Name;
        tl.Id= INR.Id;
        tl.srl= childmethod(tl.Id,LR);
        templist.add(tl);
        }
      }return templist;
    }
}



Let me know if you need more help.


Regards 
Nishant