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

How to retrieve fields from Sub query in Apex code ??
list<Account> ACC = [select id, (select ID, Name from Contacts) from Account];
From the above statement how to retrive and assign contact name to a Apex variable?
From the above statement how to retrive and assign contact name to a Apex variable?
Use the below one
Hope this helps
Please mark as best answer if the above helps ...!!
In your example, You are querying for all the Accounts and their Contact list.
Besides my OCD that tells me that variables name should be allways lowercase, ;) the way you can retrieve the contact list for a acoount is as follows: What the above code does is: it assumes you have at least one Account and the first account has contanacts. (Just following your example).
The List contactList will have all the contacts for the first retrieved Account. from there, you can iterate through the list and do whatever to the contactList.get(i).Name;
Good Luck :)
If you want all the contact names of all the accounts you cant save it in just one variable, you can create a list of contacts or a map of account and its related contacts.. I hope it helps! :)
Please try this piece of code .
Hope it's help you.
Regard
Suraj
Thanks very much for helping out.