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
Giddamreddi ReddyGiddamreddi Reddy 

how to get child record in parent obj in lookup relation ship?

Best Answer chosen by Giddamreddi Reddy
Amit Chaudhary 8Amit Chaudhary 8
Hi Giddamreddi Reddy,

Please try to execute below code in develper console. In this case with parent record you will get child contact records as well :-


List<Account> listAccount = [select id,name , (select id,firstName , LastName from contacts ) from account limit 10];
For(Account acc : listAccount )
{
   List<Contact> lstContact = acc.contacts;
   for(Contact cont : lstContact )
  {
   System.debug('Name is----'+cont.FirstName);
  }
}

NOTE: This code has not been tested and may contain typographical or logical errors

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
 

All Answers

ManojjenaManojjena
Hi Giddamreddi,
What exactly you want ,where you want inside your class or soem where else ?

Assume that you have instance of account as acc ,If you want all contacts related to that account you can achieve as below 
List<Contact> conList=acc.Contacts ;

Let me incase any doubt .


 
Amit Chaudhary 8Amit Chaudhary 8
Hi Giddamreddi Reddy,

Please try to execute below code in develper console. In this case with parent record you will get child contact records as well :-


List<Account> listAccount = [select id,name , (select id,firstName , LastName from contacts ) from account limit 10];
For(Account acc : listAccount )
{
   List<Contact> lstContact = acc.contacts;
   for(Contact cont : lstContact )
  {
   System.debug('Name is----'+cont.FirstName);
  }
}

NOTE: This code has not been tested and may contain typographical or logical errors

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
 
This was selected as the best answer