2

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

2
  • So you have a list of Accounts and want to get all of their Contacts?
    – Mike Chale
    Jun 27, 2013 at 12:37
  • 3
    If you have a list of Account Ids, why are you querying Contacts to get a map of Accounts? Are you only looking for Accounts with Contacts? Nov 9, 2013 at 19:37

1 Answer 1

5

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .