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
Aditya RautAditya 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;
    }
}
Best Answer chosen by Aditya Raut
AnkaiahAnkaiah (Salesforce Developers) 
Hi Aditya,

try with below code.
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, New List<opportunity>{op});
    }
    
    return oppmap;
    }

If this helps, Please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Aditya,

try with below code.
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, New List<opportunity>{op});
    }
    
    return oppmap;
    }

If this helps, Please mark it as best answer.

Thanks!!
 
This was selected as the best answer
syed jabeenasyed jabeena
Hi Aditya,

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
Abdul KhatriAbdul Khatri
Hi Aditya,

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:
public static Map<Id, list<opportunity>> oppmethod(Set<Id> oppId){
        
        Map<Id, List<Opportunity>> oppmap = new Map<Id, List<Opportunity>>();      
        
        for(Opportunity opp : [SELECT Id, AccountId 
                                  FROM Opportunity 
                                  WHERE Id = :oppId]){
            
            List<Opportunity> tempList = New List<Opportunity>();
            if(oppmap.containsKey(opp.AccountId))
                tempList = oppmap.get(opp.AccountId);
            
            tempList.add(opp);
            oppmap.put(opp.AccountId, tempList);
        }
                
        return oppmap;
    }

Solution 2:
public static Map<Id, list<opportunity>> oppmethod(Set<Id> oppId){
        
        List<Id> idAccountList = new List<Id>();
        Map<Id, List<Opportunity>> oppmap = new Map<Id, List<Opportunity>>();
        
        for(Opportunity opp : [SELECT Id, AccountId, Name 
                              FROM Opportunity 
                              WHERE ID IN: oppId])
        {
            idAccountList.add(opp.AccountId);
        }
        
        List<Account> accountList = [SELECT Id
                                     , (SELECT Id FROM Opportunities 
                                        WHERE Id = :oppId) 
                                     FROM Account
                                     WHERE Id = :idAccountList];
        
        for(Account account : accountList){
           
            if(account.Opportunities == null || account.Opportunities.size() == 0) continue;
            
            oppmap.put(account.Id, account.Opportunities);
        }

        return oppmap;
    }

I hope this will help you when you got into any issues from the other solutions.

Regards.