+ Start a Discussion
Ek0nomikEk0nomik 

Efficiently finding child account(s) of parent account

Using the API (i.e. SOQL), what is the fastest way to find all the child accounts from a specified account id?  Is my only option to query all accounts and their parents using something like:

 

SELECT Parent.ParentId, Parent.Parent.ParentId, Parent.Parent.Parent.ParentId, Parent.Parent.Parent.Parent.ParentId, Parent.Parent.Parent.Parent.Parent.ParentId FROM Account

 and then resursively iterate through the result set looking for accounts related to my specified account id?

Nick00000Nick00000

yes I've done this, indeed there is no way to do a heirarchical query, so its a case of doing multiple queries until you get all the children.

 

i.e.in basic terms:

 

children = select all accounts where parentId = "top account id"

then

children = select all accounts where parentId in (children)

until children is empty.

 

so the number of queries it takes is just the highest number of levels. Performance wise its fine.