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
Stephen E 4Stephen E 4 

Is it possible to grab child accounts in a single SOQL query?

For example, if I wanted to grab accounts and their contacts in a single SOQL query I could do this:

[SELECT Id,(SELECT Id FROM Contacts) FROM Account]


Is this possible to do with child accounts? (Using the Standard "Parent" field)

When I attempt this:

[SELECT Id,(SELECT Id FROM Accounts) FROM Account]

I get this error:

"Didn't understand relationship 'Accounts' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names."

Is this possible?
Best Answer chosen by Stephen E 4
Alain CabonAlain Cabon
Hi,
  1. select name,(select id,billingcity from ChildAccounts) from account
  2. select name,parent.id,parent.billingcity from account

ChildAccounts is the child relationship name.
User-added image

"__r" (custom) and plural (standard) are conventions but there are exceptions.
 

All Answers

Alain CabonAlain Cabon
Hi,
  1. select name,(select id,billingcity from ChildAccounts) from account
  2. select name,parent.id,parent.billingcity from account

ChildAccounts is the child relationship name.
User-added image

"__r" (custom) and plural (standard) are conventions but there are exceptions.
 
This was selected as the best answer
Prashant Pandey07Prashant Pandey07
Hi Stephen,

As Alain mentioned you need to use child relationship filed to get the child records.

Try below query to get the child account.
 
select id,(select id from ChildAccounts) from account

--
Thanks,
Prashant
Stephen E 4Stephen E 4
Thank you both!