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
Niki_16Niki_16 

Finding Top level createdbyuser in a user hierarchy

Is there any way to find the root level username in the users hierarchy. For example if an admin user (user1) creates a user (user2), an then that subuser (user2) creates another user (user3), then is there any way to retrieve the top level user name (user1)  using user3.

 

Please help me out, its urgent. Any help would be appreciated.

 

Thanks,

Shaveta

aalbertaalbert

In a single step, no. This data is stored in the UserRole object and those records maintain a relationship to its direct parent through the ParentRoleId. 

 

You can query all UserRole records and iterate up the hierarchy using the ParentRoleId to maintain association. Or you can iterate across many SOQL queries to walk up the role hierarchy from one node to the next.

 

Niki_16Niki_16

Thanks for the reply.

So there is not dierct way to retrieve the parent user. But iterating the hierarchy would not be helpful in my case as it requires requesting salesforce on each and every request until the parent user is found. this could make my application works slow. Is there any other alternative for this.

 

 

Regards,

Shaveta

aalbertaalbert

You could use a custom field on the User record to store the top level user. But that would need to be maintained either through a Trigger on the User object or the UI manually. 

 

 

 

Niki_16Niki_16

Yes. Thanks for the solution.

 

Shaveta.