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