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

How to add Related object records in a List/Array of same object ?
I wanted to add Related Contact records from Account Object in a Contact array. But I'm getting error in doing so.
Account[] acctsWithContacts = [SELECT Name, (SELECT FirstName,LastName FROM Contacts)FROM Account limit 10];
// Get child records
Contact[] cts = acctsWithContacts.Contacts; //(Here I'm getting the error)
System.debug('Name of first associated contact: ' + cts[0].FirstName + ', ' + cts[0].LastName);
Account[] acctsWithContacts = [SELECT Name, (SELECT FirstName,LastName FROM Contacts)FROM Account limit 10];
// Get child records
Contact[] cts = acctsWithContacts.Contacts; //(Here I'm getting the error)
System.debug('Name of first associated contact: ' + cts[0].FirstName + ', ' + cts[0].LastName);
try with below in dev cosole.
If this helps please mark it as best answer.
Thanks!!
try below code and get the reference. I believe it will work. don't forget to mark it as the best answer.
Thank you