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
Sarthak Patil 15Sarthak Patil 15 

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.
Best Answer chosen by Sarthak Patil 15
SUCHARITA MONDALSUCHARITA MONDAL

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

AbhishekAbhishek (Salesforce Developers) 
Hi,

The below developer discussion might help you,

https://developer.salesforce.com/forums/?id=906F00000008x8bIAA

Check it once.

 
SUCHARITA MONDALSUCHARITA MONDAL

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

This was selected as the best answer
Sarthak Patil 15Sarthak Patil 15
Thanks Abhishek and Sucharita.