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
Mike Lee PCLMike Lee PCL 

Apex List - storing multiple types

Hello,

I'm learning Apex and one thing I've come across is storing multiple sobject types in a List. For example:

List<Contact> myList = [SELECT ID, AccountID, FirstName, LastName, Account.Name FROM Contact];

In the above ID, AccountID, FirstName, and LastName are all fields from Contact so if I system.debug myList I will see these 4 fields. I won't however see the Account.Name field. Makes sense since the Account Name is from the Account object.

Couple of questions:

1. Since myList is of type Contact I was assuming that it wouldn't be able to store any fields that aren't from Contact. How is it that Account.Name is able to be stored in a Contact List?
2. How do I extract the Account Name from each of the records stored in myList? When I perform a SOQL query in Query Editor I see the Account Names there. But I'm at a loss as to how to extract the Names via Apex.

Thanks,

Mike
Best Answer chosen by Mike Lee PCL
venkat-Dvenkat-D
Im not sure how sfdc does that but may be sfdc has some predefined views based on object relationships that helps in this functionality. But as long as you have either lookup or Master-detail relation between two objects you can use relationship queuries to get parent and child recprds

All Answers

venkat-Dvenkat-D
List<Contact> myList = [SELECT ID, AccountID, FirstName, LastName, Account.Name FROM Contact];

You are already extracting Acount name by Account,Name in SOQL. SFDC doesnt store account name in contact object. When we use Account.Name it pulls up account name along with contact and you can refer to that by mylist[0].Account.Name or record variable in for loop
venkat-Dvenkat-D
You can find more information at below link on relationship queries
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships.htm
Mike Lee PCLMike Lee PCL
Thanks for the response venky409.

Where I'm still confused concerns the List myList. I declared it as type Contact however it seems to be storing Account.Name. Since Account.Name is from the Account object how is this happening? Shouldn't myList only be able to store fields from the Contact object? Or is it able to somehow store fields from related objects as well?

Mike
venkat-Dvenkat-D
Im not sure how sfdc does that but may be sfdc has some predefined views based on object relationships that helps in this functionality. But as long as you have either lookup or Master-detail relation between two objects you can use relationship queuries to get parent and child recprds
This was selected as the best answer
JeffreyStevensJeffreyStevens
Ya venky is correct - read through the relationships link - that will help.  It's like a join from SQL.  The relationships that are on the contact object are available to you.  Because Account is related to Contact, you could specify ANY field from the Account object in your SOQL.
venkat-Dvenkat-D
Mark as Resolved if it helped.