[SalesForce] how to get the list of accounts from Contact

i have list of Accountids .now i am query the contact based on list of AccountIds for this one i create one for loop ..how we can get the Map<Id,List<Account>>

Map<Id,List<Account>> mapcon = new Map<Id,List<Account>>();
List<Account> acclist = new List<Account>();

for(Contact con:[select id,Accountid from contact where Accountid in:Accountid]){
    acclist.add(con.Accountid);
    mapcon.put(con.id,acclist);
}

The problem is here i am get the Accountid in acclist…i want instead of Accounid how to get the list of Accounts ..any help
Thanks
Venki

Best Answer

You're on the right way. If you want to add account objects instead of ID's just use the reference name Account

for(Contact con:[Select Id, Account.Id, Account.Name, Acount.PesrsonEmail, Account.LastName 
                 From Contact 
                 Where Accountid IN:Accountid]){

    acclist.add(con.Account);
}

BUT, the relationship between Contact and Account is n-1 so one Account can have many Contacts and i think you can not get a list of accounts belongs to a contact.

Related Topic