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
andrew.hummel1.3935166496106152E12andrew.hummel1.3935166496106152E12 

Help with unknown reason for Apex class error?

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);
                }
            }
        }


NishBNishB
Hi,
     Maybe o.Inception_Renewal_Date__c is null  Can you write a system.debug statement and check whether the field has any value in it
andrew.hummel1.3935166496106152E12andrew.hummel1.3935166496106152E12
Hi NishB,

We have that field on the actual record, and i can confirm that it is indeed populated.  I cant seem to figure out where salesforce is seeing a null value.  
NishBNishB
Does the field have value for all the records ? Did you try adding a debug statement before the if condition for o.Inception_Renewal_Date__c?