• jeon lae
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 10
    Replies
Hi,
I have a requirement to distribute the amount on Parent Account equally among all opportunities. The following code is throwing NULL pointer exception. Please help.


public static void distAmountEqually(List<Opportunity> oppList, map<Id,Opportunity>oldmap, boolean isInsert, boolean isUpdate){
        Set<Id> oppAccIds = New Set<Id>();
        Map<Id,List<opportunity>> mapAccToOpps = New Map<Id,List<opportunity>>();
        Integer noOfOpps = 0;
        List<Opportunity> oppsToUpdt = New List<Opportunity>();

        for(Opportunity opp:oppList){
            oppAccIds.add(opp.accountId);
        }
        
        for(Account acc: [SELECT AnnualRevenue ,(SELECT Distributed_Amount__c FROM Opportunities) 
                          FROM Account WHERE Id IN :oppAccIds]){
            
            if(!mapAccToOpps.containsKey(acc.Id)){ 
                mapAccToOpps.put(acc.Id, new List<opportunity>());                
            }
            mapAccToOpps.get(acc.Id).addall(acc.opportunities);
        }
        
        for(Opportunity op:oppList){
            if(mapAccToOpps.containsKey(op.accountId)){
                 noOfOpps= op.account.Opportunities.size();

                 for(Opportunity opy:mapAccToOpps.get(op.accountId)){
                    opy.Distributed_Amount__c = op.Account.AnnualRevenue / noOfOpps;
                }
            }
        }
    
    }
}