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
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
Maheshh,

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.
 
Or
 
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