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
withoutmewithoutme 

Sharing over the entire account heirarchy

I manage account sharing using APEX (accountshare table). If that user is specified in the lookup (Account manager) he is given access to that account(and related contact and opportunities). But, the problem is when the account is part of a tree. Likethis

                       A

                                    |

______________

|            |                

B C

_____________

| | |

D E

 

So if a user is specified in the manager lookup in account E, I need to make sure he has full access to the entire tree:A.B.C.D.E.F. I have seen that giving the user access to A does not give him access to accounts lower down in the heirarchy automatically.

 

Any ideas? 

 

CaffeineCaffeine

Don't know who the customer is, but that is a really odd requirement.

 

But if that is the requirement, I think the solution is doing a little tree-walking like this:

 

1. Write a routine to get the parent of E and recursively go up through the Account parent tree until it reaches an Account without a parent.

 

2. Add a share for the account.

 

3. Find all accounts that have this Account as the parent.

 

4. For each of the Accounts returned repeat steps 2 - 4 until no accounts are returned.

 

This a classic tree-walking algorithm and will require an understanding of how to build recursive routines. 

 

Hope this helps. 

withoutmewithoutme
this sounds good, but while i query the DB for each level in the tree - i will certainly hit my governor limits. And on top of that, if I mass load 100 records, i will hit my "too many query" limit.
CaffeineCaffeine
You are right.  If the tree has any appreciable width or depth, you'll hit your limits.  I'm not sure there is a good answer in the APEX world right now.