+ Start a Discussion
Varun kumar SaxenaVarun kumar Saxena 

General Collections queries

Guys ,

Can anyone tell me where I mean which list type or list is used to store the below Query .
select Id , (select Id, Name from Contacts) from Account .
Best Answer chosen by Varun kumar Saxena
Vasani ParthVasani Parth
Varun,

A SOQL query will always return a list of sobjects even if a COUNT method is used  If you are assigning a query to a single sobject Apex will execute your query then attempt to assign to this sobject (unfortunately, if there are no elements in the returned query or more than one you will get an exception).

Please mark this as the best answer if this helps

 

All Answers

Vasani ParthVasani Parth
Varun,

A SOQL query will always return a list of sobjects even if a COUNT method is used  If you are assigning a query to a single sobject Apex will execute your query then attempt to assign to this sobject (unfortunately, if there are no elements in the returned query or more than one you will get an exception).

Please mark this as the best answer if this helps

 
This was selected as the best answer
VineetKumarVineetKumar
You can store the value in List<Account>
List<Account> accountList = [select Id , (select Id, Name from Contacts) from Account];
To refer contacts you will have to do something like.
accountList[0].Contacts

Do mark it as best answer if it helped.
Varun kumar SaxenaVarun kumar Saxena
Thanks Vasani,

There is one more requirement , suppose if I want to debug the name of the contact from the above SOQL parent to child query , then what should I practice to get the solution