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
SabrentSabrent 

test class error

One of my test class fails with the following error, which is highlighted in red in trigger.

 

Could it be because i have an update in the class which is called by this trigger?

 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Agreement_Update_After: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Agreement_Update_After: line 60, column 1: []

 

 

trigger Agreement_Update_After on Agreement__c (after update) { 

	if (trigger.isUpdate && trigger.new != null){
		
		 	                      
		list<Agreement__c> rid_onboard = new list<Agreement__c>();
		rid_onboard = trigger.new;
		   
		list<ID> reqids = new list<ID>();
		        
	   	for(Agreement__c rob : trigger.new){     
	      
	        if(!System.Trigger.oldMap.get(rob.Id).Approval_Status__c.equals(rob.Approval_Status__c) && rob.Approval_Status__c.equals('Approved')  && ((!rob.EngagementStatus__c.equals('Active')) || (!rob.EngagementStatus__c.equals('Probation'))) ){  	    	
	    	    reqids.add(rob.Id);
			}
	     
        }
        
        if (reqids.size() > 0 ){ 
        	
        	ctrl_SM_OverallTime.calculateOverallTime(reqids);    
     	} 
	
	}  
	


}

 

 

 

public with sharing class ctrl_SM_OverallTime { 
	
    public static void calculateOverallTime(list<ID> reqids){          
       	        
        list<Agreement__c> r1 = [ Select Id, .... 
        							From Agreement__c where Id = :reqids];
                
        list<Agreement__c> r2 = new list<Agreement__c>();
                            
        for (Agreement__c r: r1){                                               
	     
	     	if (.......){ 
	           
		        datetime tob = r.Approved_Date__c;   // this is populated in a before update trigger on  Agreement__c 
		        date dob = tob.dateGMT();
		        
		        datetime ttob = r.Exposed__c;
		        date ddob = ttob.dateGmt(); 
		       
		        r.On_Board_Cycle_Time__c = BusinesssDaysCalc.getDiffBusinessDays(ddob, dob);
	     	
	     	} else if (........){ 
	              
		       
			       if( r.Approved_Date__c != null){
				        datetime tob1 = r.Approved_Date__c;
				        date dob1 = tob1.dateGMT();
				        
				        datetime ttob1 = r.CreatedDate;
				        date ddob1 = ttob1.dateGmt(); 
				          
				        r.On_Board_Cycle_Time__c = BusinesssDaysCalc.getDiffBusinessDays(ddob1, dob1);         
			       }
	     	} else if (..........){ 
	          
		        datetime ttpa = r.Approved_Date__c;
		        date dtpa = ttpa.dateGMT();
		        
		        datetime ttpa1 = r.PA_Exposed__c;
		        date dtpa1 = ttpa1.dateGmt(); 
		         
		        r.On_Board_Cycle_Time__c = BusinesssDaysCalc.getDiffBusinessDays(dtpa1, dtpa);    
	     	}
        	
        		r2.add(r);	
	   }
    
			    if(r2.size() > 0){
			    	update r2;    /// could it be becuase of this. 			    	
			    }
  }
  

}

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

Well if your field value for 'Approval_Status__c' and / or 'EngagementStatus__c' is null, is why that error is being throw. You can always validate that those values are not equal to null as the first part of your if statement.

All Answers

Alex.AcostaAlex.Acosta

Well if your field value for 'Approval_Status__c' and / or 'EngagementStatus__c' is null, is why that error is being throw. You can always validate that those values are not equal to null as the first part of your if statement.

This was selected as the best answer
SabrentSabrent

Thanks Alex !!! That's sparked an idea. thought not a good one, will take off some frustartion for now.

 

I put the following condition in the trigger to avoid from being run in the test class.

if (!Test.isRunningTest())