function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HelloSanHelloSan 

how to populate Map<id,List<Opportunity>> i want to map account with list of Opportunities associated to that account,the below code is not working,how can we achieve this

for(Account acc : acclist)
{
    for(Opportunity opp1 : opplist)
    {
        if(acc.id == opp1.AccountId)
        {
            maptest.put(acc.id,opp1.Opportunity);
        }
    }
}
Best Answer chosen by HelloSan
Jitendra RawatJitendra Rawat
Hi,

This code will resolve your problem :
Ma<Id,List<Opportunity>> accountOppMap = new Map<Id,List<Opportunity>>();
for(Opportunity opp : [Select id,accountId from opportunity]){
    List<Opportunity> opplst = AccountOppMap.get(opp.AccountId);
    if(opplst == null)
        opplst = new List<Opportunity>();
    opplst.add(opp);
    accountOppMap.add(opp.accountId,opplst);
}

Please check it and let me know its helps you.

Thanks
Jitendra

All Answers

ManojjenaManojjena
HIi San ,

Try with below code ut will work .
 
Map<Id,List<Oportunity>> accIdWithOpplIstMap=new Map<Id,List<Oportunity>>();
For(Account acc :[SELECT id,(SELECT id,Name FROM Opportunities)FROM Account]){
  accIdWithOpplIstMap.put(acc.Id,acc.Opportunities);
}

Let me know if it helps !!
Thanks
Manoj
Shrikant BagalShrikant Bagal
Please try:

for(Account acc : acclist)
{
    for(Opportunity opp1 : opplist)
    {
        if(acc.id == opp1.AccountId)
        {
            if(!maptest.containsKey(acc.id)){
                    maptest.put(acc.id, new List<Opportunity>());
            }
              maptest.get(acc.id).put(opp1);             
        }
    }
}


If its helps, please mark as best answer so it will help to other who will serve same problem.
​Thanks! 
Jigar.LakhaniJigar.Lakhani

Hello,

It looks like you need opportunities which are related to accounts, Please try below code to avoid netsted for loop iteration. You can directly use that prepared map(mapAccountWithOpportunities) and iterate over accounts with its opportunities.

Here "acclist", can be set of account ids or list of accounts

Map<Id,Account> mapAccountWithOpportunities = new Map<Id,Account>([SELECT Id,Name,(SELECT Id,Name FROM Opportunities) FROM Account WHERE Id In:acclist]);
for (Id accountId:mapAccountWithOpportunities) {
	Account objAccount = mapAccountWithOpportunities.get(accountId);
	system.debug('############   Opportunities  ' + objAccount.Opportunities);
}

Thanks & Regards,
Jigar
Jitendra RawatJitendra Rawat
Hi,

This code will resolve your problem :
Ma<Id,List<Opportunity>> accountOppMap = new Map<Id,List<Opportunity>>();
for(Opportunity opp : [Select id,accountId from opportunity]){
    List<Opportunity> opplst = AccountOppMap.get(opp.AccountId);
    if(opplst == null)
        opplst = new List<Opportunity>();
    opplst.add(opp);
    accountOppMap.add(opp.accountId,opplst);
}

Please check it and let me know its helps you.

Thanks
Jitendra
This was selected as the best answer
Jitendra RawatJitendra Rawat
Hi, Thanks for choosing best answer. Thanks Jitendra
HelloSanHelloSan
Hi All i want to filter(Exclude) the Opportunity which is inserted or updated recently to this list . I am using below code in trigger events After Insert and After Update. With the below code i am getting the exception System.FinalException.

Ma<Id,List<Opportunity>> accountOppMap = new Map<Id,List<Opportunity>>();
for(Opportunity opp : [Select id,accountId from opportunity]){
    List<Opportunity> opplst = AccountOppMap.get(opp.AccountId);
    if(opplst == null)
        opplst = new List<Opportunity>();
    opplst.add(opp);
    accountOppMap.add(opp.accountId,opplst);
}
 
Rajender Deshpande 1Rajender Deshpande 1
Hi, Please try the below code

Apex class:
--------------
Public class Mapexample1 {
    
    Public Map<id,list<opportunity>> mapval{set;get;}
    
    Public void getmap(){
        mapval = new map<id,list<opportunity>>();    
        List<opportunity> opts= [select id,accountid from opportunity];
        if(opts.size() > 0){   
          for(opportunity opty:opts){
              mapval.put(opty.accountid,opts);
            }     
          }
        }
    
    Public void show(){
        system.debug('Size of the map' +mapval.size());
        system.debug('Size of the map' +mapval.keyset());
        system.debug('Size of the map' +mapval.values());
    }
}

Execute the below code from anonoymous window to see the results:
Mapexample1 obj=new Mapexample1();
obj.getmap();
obj.show(); 
Rayachoti RaghavendraRayachoti Raghavendra

public class oppmapidlist {
    public static void method(){
        Map<id,list<opportunity>> oppmap = new Map<id,list<opportunity>>();
        for(opportunity opp:[select id,AccountId from opportunity]){
            if(oppmap.containskey(opp.AccountId)){
                List<opportunity> oppid = oppmap.get(opp.AccountId);
                oppmap.get(opp.AccountId).add(opp);
            }else{
                oppmap.put(opp.AccountId,new List<opportunity>{opp});
            }
           
        }
       
    }
try it once
Ravi Shankar GuggilapuRavi Shankar Guggilapu
Use this piece of code:
Map<id,List<Opportunity>> accWithOpps = new Map<Id,List<Opportunity>>();
for(Opportunity opp:[SELECT Id, AccountId FROM Opportunity]){
If(!accWithOpps.containsKey(opp.AccountId)){
accWithOpps.put(opp.AccountId,new List<Opportunity>());
}
accWithOpps.get(opp.AccountId).add(opp);
}