You need to sign in to do that
Don't have an account?

How to access sub query field value in list.
Hi everybody,
I am trying to access subquery field which is in a list.
ex: List<Account> newList = new List<Account>([Select Name,phone,(Select name,phone from Contacts) from Account]);
In the above code the values can be accessed for account by using "newList.Name". But when i try to access contacts of that account i.e.(newList.Contacts.name).It is giving me error.
So,is there any way to access the subquery(Contacts) field values.
Thanks.
I am trying to access subquery field which is in a list.
ex: List<Account> newList = new List<Account>([Select Name,phone,(Select name,phone from Contacts) from Account]);
In the above code the values can be accessed for account by using "newList.Name". But when i try to access contacts of that account i.e.(newList.Contacts.name).It is giving me error.
So,is there any way to access the subquery(Contacts) field values.
Thanks.
Hi Sarthak,
It would be as follows:
List<Account> newList = new List<Account>([Select Name,phone,(Select name,phone from Contacts) from Account]);
if(newList.size()>0){
for(Account acc : newList){
for(Contact con : acc.Contacts){
//do some code
}
}
}
Thanks,
Sucharita
All Answers
The below developer discussion might help you,
https://developer.salesforce.com/forums/?id=906F00000008x8bIAA
Check it once.
Hi Sarthak,
It would be as follows:
List<Account> newList = new List<Account>([Select Name,phone,(Select name,phone from Contacts) from Account]);
if(newList.size()>0){
for(Account acc : newList){
for(Contact con : acc.Contacts){
//do some code
}
}
}
Thanks,
Sucharita