I've noticed that when using parent to child SOQL query we mark the receiving variables as if they will receive a single record but these variables will in fact receive a list of record.

as you could see below:

for(Account MyAccountList : [select id, (select id from contacts) from account where id in :accountIds])
        for(Contact MyContactList : MyAccountList.contacts)

Is there a reason I don't understand for doing this?

  • No it is not the case always. There is no mandate on receiving end, you can have single object or can have list as well.
    – Ysr Shk
    Aug 12, 2020 at 9:44
  • If you look at the syntax of the for loop it receives a list as a parameter and iterates through them one by one, so the MyAccountList is an inappropriate variable name here, it should be myAccount. The child relationship query always return a list. It would be nice if you could reframe your question and make more clear. Aug 12, 2020 at 9:45
  • yes, it makes sense thanks @rahulgawale
    – Jojosfdc
    Aug 12, 2020 at 11:42

1 Answer 1


It's Because there may be a chance in the future when there will be more than one record. In that case if your logic running for a single record then your functionality fails. Due to that salesforce always return list so that if there is more than single record it will not fail.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .