I am new to salesforce development and want to achieve the below requirement.

We follow account hierarchy and I want to show all the opportunities, contacts of child account on parent account related list using VF.

I know we cannot query parent to child till 1 level deep. Please help how can I achieve this.

Parent Account | Child Account | Associated opportunities, contacts



Unaware of how you want to use the data, the following query will retrieve the data needed.

List<Account> accounts = [
    select id, 
        (select Id from Cases), 
        (select Id from Contacts), 
        (select Id from Opportunities) 
    from Account
    WHERE id = '<Root Account ID>'
    or Account.ParentId = '<Root Account ID>'

I have placed an oroperator in my SOQL to illustrate how you can use the Account hierarchy.

The following debug shows you the collections of Cases, Contacts and Opportunites queried.

for (Account a : accounts) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.