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
SivarajanSivarajan 

Role Hierarchy check in Apex class

 

Hi Everyone,

 

 

I would like to check logged user role with a role of particular record owner. If logged user role is on top of the record owner user role then I would like to show a visualforce page. any idea How to do that it very simply ?

 

Thanks in advance !

Best Answer chosen by Admin (Salesforce Developers) 
ca_peterson_oldca_peterson_old

There's actually a pre-built group sObject used for sharing rules that you might be able to use for this. For every role there's a group object with "RoleAndSubordinates" as it's type and the relatedId field as your higher role. You can query the groupMembers of this record and see if your other role is included. If so, it's lower in the hierarchy, otherwise it's not.

All Answers

spraetzspraetz

I don't believe there is currently any way to do that.

 

Interesting use case, though.  And let us know if you find a way to find that information.

 

ca_peterson_oldca_peterson_old

There's actually a pre-built group sObject used for sharing rules that you might be able to use for this. For every role there's a group object with "RoleAndSubordinates" as it's type and the relatedId field as your higher role. You can query the groupMembers of this record and see if your other role is included. If so, it's lower in the hierarchy, otherwise it's not.

This was selected as the best answer
magdielmagdiel

Not sure about proposed solution, tested this one and worked, dropped here for future reference, 

 

 

    Map<String, UserRole> roleMap  = new Map<String, UserRole>();

	public Boolean evalRoleHierarchy(String uManagerRoleId, String uRoleId, String uParentRoleId)
	{
		if(uManagerRoleId == uParentRoleId)
			return true;
			
		Boolean resVal = false;
        roleMap = new Map<String, UserRole>([SELECT Id, Name, parentRoleId FROM UserRole]); 			 
		
		// Evaluate if user role is child of manager role
		resVal = traverseRoleHierarchy(uManagerRoleId, uRoleId, uParentRoleId);
		
		roleMap.clear();
		return resVal;
	}
	
	public Boolean traverseRoleHierarchy(String uManagerRoleId, String uRoleId, String uParentRoleId)
	{
		if(roleMap!=null && roleMap.containsKey(uParentRoleId))
		{
			UserRole uRole = roleMap.get(uParentRoleId);
			if( uManagerRoleId == uParentRoleId || uManagerRoleId == uRole.ParentRoleId)
				return true;
			else
			{
				return traverseRoleHierarchy( uManagerRoleId, uParentRoleId, uRole.ParentRoleId);					
			}
		}
		
		return false;
	}

String managerRoleId = '00E8...'; // role id you want to check if is manager in a higher role hierarchy for a user
String userRoleId = '00E8...'; // your user role id
String userParentRoleId = '00E8...'; // your user's direct parent role id UserRole.ParentRoleId

system.debug(' \n\n isManager='+evalRoleHierarchy(managerRoleId,userRoleId, userParentRoleId)+' \n\n');

 

Bryce CBryce C
I wrote this code into a class, it checks to see if the logged-in user is above the hierarchy for the role id and user id passed.
static Map<Id,UserRole> all_roles;
    public Boolean recordHierarchyCheck(String checkRoleId, String checkOwnerId)
    {
        // If we are checking the same role as the logged-in user, it is not considered 'above' unless the user is the owner.
        if( checkRoleId == UserInfo.getUserRoleId() ){ return UserInfo.getUserId() == checkOwnerId; } 
        
        if( all_roles == NULL ){ all_roles = new Map<Id,UserRole>([SELECT Id, ParentRoleId FROM UserRole]); } // Get the roles.        
        if( !all_roles.containsKey(checkRoleId) ){ return false; } // Return false if an invalid role was provided.
       	UserRole role = all_roles.get(checkRoleId); // Otherwise get the role we are checking.
        
        // if there is no parent role, the checked role is the ceo and no one is above them in heirarchy except themselves (which we checked for in the prior step).
        if( role.ParentRoleId == NULL ){ return False; } 
        
        // Loop through them until the end of the hierarchy is reached, or the user's role is found as a parent.
        do {
			role = all_roles.get(role.ParentRoleId); // Advance to the next parent.
        }
        // End the loop when the parent is null (CEO is reached) or the current user's role is reached.
        while ( role.ParentRoleId != NULL && role.Id != UserInfo.getUserRoleId() );
            
        // Now that the loop has ended, either we reached the end of the hierarchy (no match to user) or we found the current user's role (in which case they are above).
        return  role.Id == UserInfo.getUserRoleId();
    }