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
Salesforce BlitzSalesforce Blitz 

Need to get owners manager details based on Role Hierarchy

Assume VP-->Lead-->Rep hierarchy structure.
Owner may be Lead/Rep. Need to get details of VP for both of them.
If owner is Rep---Need the user in VP(2 levels up)(assuming only one user per role VP wise as set in org).
If Owner is Lead---Need the user in VP(1 level up)(assuming only one user per role VP wise as set in org)

Please find 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>();
        parentAndChildren.put(ur.ParentRoleId, children);
    return getSubordinateRoles(roleId, parentAndChildren);

Any help would be appreciable