Skip to main content
Karen Crowley ha preguntado en #Apex
I have a line of code like this:

Map<Id,Account> contactMap = new Map<Id,Account>();

I want to keep this code, but I also want two more field values, so that values for Account ID, Title, and AssistantName are collected on a Contact record. Any tips on how to do this? I'm fairly new to Apex coding. Thank you!
2 respuestas
  1. 8 mar 2017, 19:54
    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;

    }

    }

     
0/9000