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
trictric 

How do I access child records

 

Hi Friends,

 

Below given is the code ,I need to access the contacts last name assiciated with the acocunts.I am  not able to .Can somebody tell me how to access the contacts asscoaied wih the account.How do I display lastname and name of the contacts asscoaited with the particular account.

 

trigger Trigger1 on Account (before insert,before update) {

account []a=[SELECT Name,(SELECT LastName,name FROM Contacts)FROM Account];

  //account []acc =[Select a.Name, a.Id, (Select AccountId, LastName, FirstName From Contacts) From Account a];

for(Account p:a)
{

System.debug('The values in the list account is ........................'+p.name);
//System.debug('The values in the list account  related contact lastname is ........................'+p.lastname);
}
}

Thanks,

trick

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Abhinav GuptaAbhinav Gupta

It would be something like this. Though I suggest loading only those accounts fired in the trigger, this code is at risk.

 

account []a=[SELECT Name,(SELECT LastName,name FROM Contacts)FROM Account];
for (account acc : a) {
 for (Contact c: a.Contacts) {
   System.debug(c.LastName);
 }

}

 

All Answers

Abhinav GuptaAbhinav Gupta

It would be something like this. Though I suggest loading only those accounts fired in the trigger, this code is at risk.

 

account []a=[SELECT Name,(SELECT LastName,name FROM Contacts)FROM Account];
for (account acc : a) {
 for (Contact c: a.Contacts) {
   System.debug(c.LastName);
 }

}

 

This was selected as the best answer
trictric

Thanks  a lot ,

Trick

 

Ankit AroraAnkit Arora

Please mark Abhinav's post as solution if it helped you. So others may get benefit.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page