Hi Karen, I guess you are fetching the Account from databases right - if that's the case, in your query try to fetch the Contact related details as well. After that, from Account - you can refer to a list as Account.Contacts. Size of Account.Contacts list could be >= 0 depending upon your Account and it's contacts counts.See below a sample code.
//This queries all Contacts related to the incoming Account records in a single SOQL query.
//This is also an example of how to use child relationships in SOQL
List<Account> accountsWithContacts = [select id, name, (select id, salutation, description,
firstname, lastname, email from Contacts)
from Account];
// For loop to iterate through all the queried Account records
for(Account a: accountsWithContacts){
// Use the child relationships dot syntax to access the related Contacts
for(Contact c: a.Contacts){
System.debug('Contact Id[' + c.Id + '], FirstName[' + c.firstname + '], LastName[' + c.lastname +']');
c.Description=c.salutation + ' ' + c.firstName + ' ' + c.lastname;
}
}
Hi Ashish,Belated thanks for your response!I am passing information from a Web form (using Wufoo - not the Salesforce forms) to create an Account and a Contact. So I'm not fetching Accounts. A former colleague sent the content below as a framework (I know the syntax is off for some of this, but I haven't had a chance to play with it yet.) Just wanted to tell you I appreciate your quick reply. I'll update this later if/when I have a chance to work on it further!*******************************************public class currentApexClass{ Account a = [Select Id From Account Limit 1].Id; formContents fc = new formContents(); fc.Id = a.Id; fc.title = "CEO"; fc.assistantName = "Joe Smith"; Map<Id,formContents> m = new Map<Id,formContents>(); m.put(a.Id,fc); class formContents{ public Id accountId {get;set;} public String title {get;set;} public String assistantName {get;set} }}
2 answers