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
TrickTrick 

Doubt about Simple query

I am trying to run below given query.I have created a trigger on the  contact object.I have parent to child query.

How do I access the last names associated with each account retrieved by the query.I have stored records which have been retrieved by the below query in the account object.What is the way to access contacts lastnames.Can somebody help.

 

Thanks.

Trick

 

trigger chuma on Contact (after insert) 
{

list<account> t=[SELECT Name, (SELECT LastName FROM Contacts) FROM Account];

System.debug('The total value in the T type variable is'+t);

 

}

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

Take a look at my previous post. It containts all the code, but I'll explain it:

 

When you do a Parent-Child sub-query in the Select statement, each parent record returned will have a List of the children objects.

For example, your query which is doing this: Select a.Name, a.Id, a.AccountNumber, (Select LastName From Contacts) From Account a

 

For this SOQL example, each Account record returned will have a List of Contact records. You access the list of Contact records by calling the relationship on the record like this: List<Contact> accountContacts = accountRecord.Contacts;

 

Using the list of Contacts, you can then iterate over the list and access the LastName for each of the contacts.

 

 

 

 

All Answers

MLamb2005MLamb2005

Do you have a field called "LastName" on your Account object? If so, you need to specific "LastName__c" in your query to access that field. Have you tried executing this code? Is it giving you any errors.

 

I'm not entirely clear what you're trying to accomplish here, can you give more explanation about what you're trying to accomplish by retrieving the last name? What is the trigger supposed to do?

Cory CowgillCory Cowgill

list<account> t=[SELECT Name, (SELECT LastName FROM Contacts) FROM Account];
for(Account a : t)

{
      List<contact> contacts = a.Contacts;

      for(Contact c : contacts)

      {

           system.debug('Account ' + a.Name + ' Contact Last Name ' + c.LastName);

       }

}

System.debug('The total value in the T type variable is'+t);

TrickTrick

I am only trying to understand how the previous query will work.I do not have lastname field on the account.

 

All I am trying to do is access the lasnames of contacts associated with each account.When I use system.debug statement I only see account names and I do not see lastnames of the contacts associated with accounts.

 

I am using parent to child query. So how do I see contact information related to the account in the parent to child query ?.

 

Thanks,trick

 

Cory CowgillCory Cowgill

Take a look at my previous post. It containts all the code, but I'll explain it:

 

When you do a Parent-Child sub-query in the Select statement, each parent record returned will have a List of the children objects.

For example, your query which is doing this: Select a.Name, a.Id, a.AccountNumber, (Select LastName From Contacts) From Account a

 

For this SOQL example, each Account record returned will have a List of Contact records. You access the list of Contact records by calling the relationship on the record like this: List<Contact> accountContacts = accountRecord.Contacts;

 

Using the list of Contacts, you can then iterate over the list and access the LastName for each of the contacts.

 

 

 

 

This was selected as the best answer
TrickTrick

Thanks a lot Cory,

 

You were of great help.I am 100% clear on how to use such a query.

 

Thanks, once again