Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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 answers
  1. Mar 8, 2017, 7:54 PM
    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;

    }

    }

     
  2. Mar 14, 2017, 8:55 PM
    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}

       }

    }

     
0/9000