0
list<account> a=[select id,type , (select id ,name from contacts),name from account limit 5];

for(account ac:a){
    system.debug('account:'+ac);

    for(contact c:ac.contact){
        system.debug('contact:'+c);
    }
}
1
  • "test", it may be easier to help you if you include more precise information such as the error and why you're not sure how to advance. Having a better understanding of your problem will help us help you better Commented Jun 3, 2015 at 8:36

3 Answers 3

-1

try this

 list<account> a=[select id,type , (select id ,name from contacts),name from account limit 5];
   for(account ac:a)
    {
        system.debug(ac);
        for(contact c:ac.contacts)
        {
            system.debug('contact:'+c);
        }
    }
3

you are querying child records in the inner query. And to get all child records of a parent record you have to use the child relationship name as used in inner query.

So, instead of

"for(contact c:ac.contact)"

use

for(contact c:ac.contacts)

Hope this may help.

0

list a=[select id,type , (select id ,name from contacts),name from account limit 5];

for(account ac:a){ system.debug('account:'+ac);

for(contact c:ac.contacts){
    system.debug('contact:'+c);
}

}

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