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
FGNYFGNY 

How to retrieve all child objects from any level in Account hierarchy

We use hierarchies in our SF organization with accounts related to higher level accounts by ParentAcccount relationship. But we need informations of all objects from child accounty of any level which are under a selected Account

For example: This query provides all opportunities of an Account:
Code:
sforce.connection.query("SELECT Id, Name, Amount FROM Oportunity 
WHERE Account.Id = " + '{!Account.Id}');

When I want all opportunities from direct child accounts of a defined parent account i can retrieve it this way:
Code:
sforce.connection.query("SELECT Id, Name, Amount FROM Oportunity 
WHERE Account.ParentId = " + '{!Account.Id}');

 However I need a query which retrieves all opportunities from an account + all opportunities of child acconts, regardless if direct  childs or "childs of childs". Is it a way to accomplish it



werewolfwerewolf
There's no single query you can use to do this.  You'll have to make a nested query – a recursive method would probably be best to do it.