Activity Forums Salesforce® Discussions How to debug the child values in parent to child query using for loop?

  • How to debug the child values in parent to child query using for loop?

    Posted by Audrey on April 21, 2016 at 6:37 am

    List<Account> accs=[select id,name,Phone,
    (select id,lastname,firstname,email
    from contacts)
    from account];

    for(Account aa:accs){
    System.debug(aa.id);
    System.debug(aa.name);
    System.debug(aa.phone);
    }
    for(Contact con:accs.contacts){

    }

    The first loop runs error free while the second loop throws up some error. I have the parent values in the first loop but i need the child values in it as well. Can anyone help?

    shariq replied 5 years, 7 months ago 4 Members · 3 Replies
  • 3 Replies
  • Naman

    Member
    April 21, 2016 at 8:14 am

    What error you are getting in second for loop?

    As multiple contacts could be associated with single account so, to traverse the contact you would need second for loop inside first. Debug contact values in second for loop. you can do like:

    for(Account aa:accs){
    System.debug(aa.id);
    System.debug(aa.name);
    System.debug(aa.phone);

    for(Contact con:accs.contacts){

    System.debug(con.email);

    }
    }

  • Parul

    Member
    September 20, 2018 at 12:20 am

    Hello,

    Could you let me know the error??

    Thanks

  • shariq

    Member
    September 20, 2018 at 7:52 pm

    Hi,

    Could you tell me your requirements that what you are trying to do.

    thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos