• andrew.hummel1.3935166496106152E12
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Our Organization has a custom object known as "Intiatives" that has connections to multiple opportunities via a Lookup Field.  We use a Method (see below) to  calculate the value of all renewal opportunities attached to the Initiative object.

This functionality had been working fine until recently when we started receiving an error for a specific reocrd type:

"Error:Apex trigger OpportunityTriggers caused an unexpected exception, contact your administrator: OpportunityTriggers: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Class.OpportunityTriggersMgr.populateInitiativeRenewals: line 233, column 1"

(Line 233 has been underlined in the method below) The only thing changed has been some sharing rule adjustment. Unfortunately we no longer work with the developer who authored the below method.  Any help or guidance on this would be GREATLY appreciated!!! 


public static void populateInitiativeRenewals(List<Opportunity> newOpp, List<Opportunity> oldOpp){
       
        Integer currentYr   = system.now().year();
        Set<Id> oppIds = new Set<Id>();
        Set<Id> initIds = new Set<Id>();
        Map<Id, List<Opportunity>> initOppMap = new Map<Id, List<Opportunity>>();
        Map<Id, Initiative__c> initMapUpdate = new Map<Id, Initiative__c>();
       
        if(newOpp != null){
            for(Opportunity o : newOpp){
                if(o.Initiative__c != null){
                    oppIds.add(o.Id);
                    initIds.add(o.Initiative__c);
                    List<Opportunity> tempOpp = new List <Opportunity>();
                    tempOpp.add(o);
                    initOppMap.put(o.Initiative__c, tempOpp);
                }
            }
           
            for(Opportunity o : [Select Id, Name, Amount, Inception_Renewal_Date__c, Initiative__c, StageName from Opportunity where Initiative__c IN: initIds AND Id NOT IN: oppIds]){
                if(o.Inception_Renewal_Date__c.year() == currentYr && o.Initiative__c != null){
                    if(initOppMap.get(o.Initiative__c) != null){
                        initOppMap.get(o.Initiative__c).add(o);
                    }
                }
               
            }
           
            for(Opportunity o : newOpp){
                if(o.Initiative__c != null){
                    Decimal wonAmt   = 0;
                    Decimal pipeline = 0;
                    for(Opportunity opp : initOppMap.get(o.Initiative__c)){
                        if(opp.StageName == 'Renewed'){
                            wonAmt += opp.Amount;
                        }
                        if(opp.StageName == 'Due for Rerate' || opp.StageName == 'Rerated' || opp.StageName == 'Renewal Produced'){
                            pipeline += opp.Amount;
                        }
                    }
                    Initiative__c initiative = new Initiative__c(id = o.Initiative__c);
                    initiative.Renewal_Won_YTD__c = wonAmt;
                    initiative.Renewals_Pipepline__c = pipeline;
                    initMapUpdate.put(initiative.Id, initiative);
                }
            }
        }


Our Organization has a custom object known as "Intiatives" that has connections to multiple opportunities via a Lookup Field.  We use a Method (see below) to  calculate the value of all renewal opportunities attached to the Initiative object.

This functionality had been working fine until recently when we started receiving an error for a specific reocrd type:

"Error:Apex trigger OpportunityTriggers caused an unexpected exception, contact your administrator: OpportunityTriggers: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Class.OpportunityTriggersMgr.populateInitiativeRenewals: line 233, column 1"

(Line 233 has been underlined in the method below) The only thing changed has been some sharing rule adjustment. Unfortunately we no longer work with the developer who authored the below method.  Any help or guidance on this would be GREATLY appreciated!!! 


public static void populateInitiativeRenewals(List<Opportunity> newOpp, List<Opportunity> oldOpp){
       
        Integer currentYr   = system.now().year();
        Set<Id> oppIds = new Set<Id>();
        Set<Id> initIds = new Set<Id>();
        Map<Id, List<Opportunity>> initOppMap = new Map<Id, List<Opportunity>>();
        Map<Id, Initiative__c> initMapUpdate = new Map<Id, Initiative__c>();
       
        if(newOpp != null){
            for(Opportunity o : newOpp){
                if(o.Initiative__c != null){
                    oppIds.add(o.Id);
                    initIds.add(o.Initiative__c);
                    List<Opportunity> tempOpp = new List <Opportunity>();
                    tempOpp.add(o);
                    initOppMap.put(o.Initiative__c, tempOpp);
                }
            }
           
            for(Opportunity o : [Select Id, Name, Amount, Inception_Renewal_Date__c, Initiative__c, StageName from Opportunity where Initiative__c IN: initIds AND Id NOT IN: oppIds]){
                if(o.Inception_Renewal_Date__c.year() == currentYr && o.Initiative__c != null){
                    if(initOppMap.get(o.Initiative__c) != null){
                        initOppMap.get(o.Initiative__c).add(o);
                    }
                }
               
            }
           
            for(Opportunity o : newOpp){
                if(o.Initiative__c != null){
                    Decimal wonAmt   = 0;
                    Decimal pipeline = 0;
                    for(Opportunity opp : initOppMap.get(o.Initiative__c)){
                        if(opp.StageName == 'Renewed'){
                            wonAmt += opp.Amount;
                        }
                        if(opp.StageName == 'Due for Rerate' || opp.StageName == 'Rerated' || opp.StageName == 'Renewal Produced'){
                            pipeline += opp.Amount;
                        }
                    }
                    Initiative__c initiative = new Initiative__c(id = o.Initiative__c);
                    initiative.Renewal_Won_YTD__c = wonAmt;
                    initiative.Renewals_Pipepline__c = pipeline;
                    initMapUpdate.put(initiative.Id, initiative);
                }
            }
        }