You need to sign in to do that
Don't have an account?
Aditya Raut
How to put a List in the value of Map?
In the below class, how to put the List(oppList) in the value of Map(oppMap)?
public class class1{
public static Map<Id, list<opportunity>> oppmethod(Set<Id> oppId){
List<Opportunity> oppList = [SELECT Id, AccountId, Name FROM Opportunity where ID IN: oppId];
Map<Id, list<opportunity>> oppmap = new Map<Id, list<opportunity>>();
for(Opportunity op : oppList){
oppmap.put(op.AccountId, op);
}
return oppmap;
}
}
public class class1{
public static Map<Id, list<opportunity>> oppmethod(Set<Id> oppId){
List<Opportunity> oppList = [SELECT Id, AccountId, Name FROM Opportunity where ID IN: oppId];
Map<Id, list<opportunity>> oppmap = new Map<Id, list<opportunity>>();
for(Opportunity op : oppList){
oppmap.put(op.AccountId, op);
}
return oppmap;
}
}
try with below code.
If this helps, Please mark it as best answer.
Thanks!!
All Answers
try with below code.
If this helps, Please mark it as best answer.
Thanks!!
This code will resolve your problem :
Map<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 if it helps you,Mark As a best Answer.
Thanks & Regards
S.Jabeena
Here are two solutions for the same requirement you need to accomplish. Any solution will work for all scenarios. Your choice which you want to pick.
Solution 1:
Solution 2:
I hope this will help you when you got into any issues from the other solutions.
Regards.