+ Start a Discussion
Maheshh PatelMaheshh Patel 

Need to query all the Account records which are part of Account Hierarchy?

Hi All,

Please suggest on how to queryall the Account records which are part of the AccountHierarchy(Parent<-Parent<-Parent->Child->Child->Child Relationship).

Thanks for the Info,
Mahesh Patel
Vasani ParthVasani Parth

If you have a small set of Accounts or you can add some other filter criteria, you can query all the Accounts and then use a recursive method to create a hirarchy.
you can also use this query: 
Select Parent.Parent.Parent.Parent.Parent.ParentId, Parent.Parent.Parent.Parent.ParentId, Parent.Parent.Parent.ParentId, Parent.Parent.ParentId, Parent.ParentId, ParentId From Account
to fetch parents till 5 level to get parents upto 5 level above. Have not tried this query, but it should work.

More reference :
  1.  http://salesforce.stackexchange.com/questions/14445/account-hierarchy-with-variable-levels-of-parents
  2.  http://salesforce.stackexchange.com/questions/105056/account-hierarchy-trigger-update-all-accounts-in-hierarchy (http://salesforce.stackexchange.com/questions/14445/account-hierarchy-with-variable-levels-of-parents)
Please mark this as the best answer if this helps