Single SOQL query to fetch account and its related contacts in salesforce - SalesforceGlobe4u
Solution: Use the SOQL query as shown below
Select id,name,(select id,name from Contacts) from Account
Example: Run the following piece of code in execute anonymous window of Developer Console
List<Account> lstAccount = new List<Account>();
lstAccount = [Select id,name,(select id,name from contacts) from Account];
for(Account acc :lstAccount) {
system.debug('Account Name: '+acc.Name);
for(Contact con: acc.contacts) {
system.debug('Contact Name: '+con.Name);
}
}
Output:
No comments:
Post a Comment