You need to sign in to do that
Don't have an account?
HelloSan
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);
}
}
}
{
for(Opportunity opp1 : opplist)
{
if(acc.id == opp1.AccountId)
{
maptest.put(acc.id,opp1.Opportunity);
}
}
}
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
Try with below code ut will work .
Let me know if it helps !!
Thanks
Manoj
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!
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
Thanks & Regards,
Jigar
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
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);
}
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();
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
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);
}