• Kamna Jain 8
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi All,

I wrote to code to trigger to calculate the total opportunity amount on Account but the trigger is not working for After Insert event. It throwing the below error. 

"AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object:"

Here is my trigger:

trigger Opportunity_AIUD on Opportunity (after insert, after update, after delete) {
    Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
    Map<Id, Opportunity> OldList = new Map<Id, Opportunity>();
    Set<Id> acctIds = new Set<Id>();
    List<Opportunity> opptyList = new List<Opportunity>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Opportunity oppty : trigger.New){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    
    If(trigger.isafter){
    if(trigger.isDelete){
        for(Opportunity oppty : trigger.old){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
                system.debug('*******acctIds*******'+acctIds);
             
            }
        }    
    }
  }
    
   
        System.debug('*****acctIdOpptyListMap ***'+acctIdOpptyListMap);
        System.debug('*********acctIds.size()***'+acctIds.size());
  
        if (acctIds.Size()>0){
        opptyList = [SELECT ID,Amount,AccountId FROM Opportunity WHERE AccountId IN : acctIds];
        System.debug('*****opptyList'+opptyList);
        for(Opportunity oppty : opptyList){
            if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
                acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
                System.debug('acctIdOpptyListMap1'+ acctIdOpptyListMap);
            }
            acctIdOpptyListMap.get(oppty.AccountId).add(oppty); 
            System.debug('acctIdOpptyListMap2'+ acctIdOpptyListMap);
        } 
        
        List<Account> acctList = new List<Account>();
        acctList = [SELECT Total_Opportunity_Amount__c FROM Account WHERE Id IN: acctIds];
        System.debug('*******acctList' + acctList);
        if (acctList.size()>0){
        for(Account acct : acctList){
            List<Opportunity> tempOpptyList = new List<Opportunity>();
            tempOpptyList = acctIdOpptyListMap.get(acct.Id);
            System.debug('******** tempOpptyList' +tempOpptyList);
            Double totalOpptyAmount = 0;
            for(Opportunity oppty : tempOpptyList){
                if(oppty.Amount != null){
                    totalOpptyAmount += oppty.Amount;
                    System.debug('********totalOpptyAmount' +totalOpptyAmount);
                }
            }
            acct.Total_Opportunity_Amount__c = totalOpptyAmount;
        }
      }
        if (acctList.size()>0){
        update acctList;
        }
    }

}

I will appreciate any help. Thanks.