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

Invalid foreign key relationship: Account.contact
Hi,
LIST<Account> Alist=[SELECT id,name,(SELECT contact.FirstName,Contact.Lastname FROM account.contacts) FROM account
Where industry='environmental'];
System.debug('The Account Details:'+Alist[0]);
System.debug('The Account Name is:'+Alist[0].Name);
System.debug('The Associated Contact FirstName is:'+Alist[0].contact.FirstName);
In the above code through a error:Invalid foreign key relationship: Account.contact
it is Parent to child retrieve single record.
Thanks
Ramesh
It should be :
System.debug('The Associated Contact FirstName is:'+Alist[0].contacts[0].FirstName);
There are multiple contacts in a single account.
Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
I had the code below it didn't work. After I saw your post I realized that the index for child object is missing.
--------wrong code, missing index---
List<Account> accs = [SELECT Name,(SELECT LastName FROM Contacts) FROM Account];
System.debug(accs[0].Contacts.LastName);
--------right code, with index------------
List<Account> accs = [SELECT Name,(SELECT LastName FROM Contacts) FROM Account];
System.debug(accs[0].Contacts[0].LastName);
---------------------
Hope this helps more beginners.