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
Manish Tripathi 20Manish Tripathi 20 

I have Scenario like I have to all Opportunity individual opportunity amount on Account different different Amount fields , I am trying with code , But not understanding the error , any help would be appreciable

public class ShowAllOpportunityAMountTOAccount {
    
    public static void getOpportunities(List<Opportunity> opportunityList){
        set<ID> setOFAccId=new set<ID>();
        for(Opportunity opp:opportunityList){
            setOFAccId.add(opp.AccountId);
        }
        Map<ID,Opportunity> mapOFOpp=new Map<Id,Opportunity>();
        List<Opportunity> opplist=[select id,Name,AccountId,Amount from Opportunity  where AccountID IN:setOFAccId ORDER BY Name DESC];
        for(Opportunity op:opplist){
            mapOFOpp.put(op.AccountId,op); 
        }
        //  system.debug(''+mapOFOpp[0].get(o.Amount););
        list<Opportunity> oplist=new List<Opportunity>();
        for(Opportunity o:opportunityList){
            Account acc=new Account();
            acc.Id=o.AccountId;
            acc.Opp1_Amount__c=mapOFOpp[0].get(o.Amount);
            acc.Opp2_Amount__c=mapOFOpp[1].get(o.Amount);
            acc.Opp3_Amount__c=mapOFOpp[2].get(o.Amount);
        }
    }
}

Error: Expression must be a list type: Map<Id,Opportunity>
Maharajan CMaharajan C
Hi Manish,

Please try the below code:
 
public class ShowAllOpportunityAMountTOAccount {
    
    public static void getOpportunities(List<Opportunity> opportunityList){
        set<ID> setOFAccId=new set<ID>();
        
        Map<ID,List<Opportunity>> mapOFOpp=new Map<Id,List<Opportunity>>();
        for(Opportunity opp:opportunityList){
            if(!mapOFOpp.containsKey(opp.AccountId)){
                mapOFOpp.put(opp.AccountId, new List<Opportunity>{opp});
            }
            else{
                mapOFOpp.get(opp.AccountId).add(opp);
            }
        }        
        
        List<Account> acclist = new List<Account>();
        for(Id  i : mapOFOpp.keySet()){
            if(mapOFOpp.containsKey(i)){
                Account acc = new Account(id = i);
                List<Opportunity> opplist = mapOFOpp.get(i);
                acc.Opp1_Amount__c=(opplist.size() > 0 || opplist.size() == 1) ? opplist[0].Amount:null;
                acc.Opp2_Amount__c=(opplist.size() > 1 || opplist.size() == 2) ? opplist[1].Amount:null;
                acc.Opp3_Amount__c=(opplist.size() > 2 || opplist.size() == 3) ? opplist[2].Amount:null;
                acclist.add(acc);
            }
        }
        
        if(!acclist.isEmpty())
            update acclist;
    }
}

Thanks,
Maharajan.C
Suraj Tripathi 47Suraj Tripathi 47
Hii Manish,

Please use below code:
 
public class ShowAllOpportunityAMountTOAccount {
    
    public static void getOpportunities(List<Opportunity> opportunityList){
        
        Map<ID,List<Opportunity>> accountIdVsOppMap = new Map<Id,List<Opportunity>>();
        for(Opportunity eachopp : opportunityList){
            if(!accountIdVsOppMap.containsKey(eachopp.AccountId)){
                accountIdVsOppMap.put(eachopp.AccountId, new List<Opportunity>{eachopp});
            }
            else{
                accountIdVsOppMap.get(eachopp.AccountId).add(eachopp);
            }
        }        
        
        List<Account> accountList = new List<Account>();
        for(Id  eachId : accountIdVsOppMap.keySet()){
            if(accountIdVsOppMap.containsKey(eachId)){
                Account accountObj = new Account(id = eachId);
                List<Opportunity> opplist = accountIdVsOppMap.get(eachId);
                accountObj.Opp1_Amount__c = (opplist.size() > 0 || opplist.size() == 1) ? opplist[0].Amount:null;
                accountObj.Opp2_Amount__c = (opplist.size() > 1 || opplist.size() == 2) ? opplist[1].Amount:null;
                accountObj.Opp3_Amount__c = (opplist.size() > 2 || opplist.size() == 3) ? opplist[2].Amount:null;
                accountList.add(accountObj);
            }
        }
        
        if(!accountList.isEmpty())
            Update accountList;
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Suraj