+ Start a Discussion
Warren Wade, AEWarren Wade, AE 

After Delete error - Roll Up Trigger - "Attempt to de-reference a null object"

'm working on a trigger for an org I do some volunteering for and I'm stuck (because I'm not good at this).  
Preservation_Object__c is the Master object and Preservation_Activities__c is the child account

It's supposed to update the parent object with the Activity_Type__c of most recent Activity_Date__c

When the Child record is updated or inserted, it works like it's supposed to.

With the trigger.isdelete added, I'm getting an "Attempt to de-reference a null object" error at line 5. 

Any thoughts on how this could get fixed?
trigger RollUpRecentActivity on Preservation_Activities__c (after insert, after update, before delete) {
    set<Id>setPreserveId = new set<Id>(); 
    list<Preservation_Object__c>listPreserve = new list<Preservation_Object__c>(); 
    if(trigger.isInsert || trigger.isUpdate || trigger.isdelete){
        for(Preservation_Activities__c oActivities:trigger.new){ 
            setPreserveId.add(oActivities.Preservation_Object__c); 
        }
    }
    system.debug('@developer-->setPreserveId: '+setPreserveId);
    if(setPreserveId.size()>0){ 
        for(Preservation_Activities__c oActivities:[
            Select Preservation_Object__c,id,Name,Activity_Type__c,Activity_Date__c  
            from Preservation_Activities__c 
            where Preservation_Object__c In:setPreserveId and Activity_Date__c != null and isDeleted = FALSE
            order by Activity_Date__c desc limit 1]){
                Preservation_Object__c oPreserve = new Preservation_Object__c();
                oPreserve.Id = oActivities.Preservation_Object__c;
                oPreserve.Last_Activity_Type__c = oActivities.Activity_Type__c;
                listPreserve.add(oPreserve);
            }
        system.debug('@developer-->listPreserve: '+listPreserve); 
        if(listPreserve.size()>0){ 
            update listPreserve;
        }
    }
}

 
Nithesh NNithesh N
Hi Warren, 

Whenever you use trigger.isdelete , You should always use Trigger.Old. 
As records which are about to be deleted, will be available in Trigger.Old, not in trigger.new.
trigger RollUpRecentActivity on Preservation_Activities__c (after insert, after update, before delete) {
    set<Id>setPreserveId = new set<Id>(); 
    list<Preservation_Object__c>listPreserve = new list<Preservation_Object__c>(); 
    if(trigger.isInsert || trigger.isUpdate){
        for(Preservation_Activities__c oActivities:trigger.new){ 
            setPreserveId.add(oActivities.Preservation_Object__c); 
        }
    }
    
    else if(trigger.isdelete){
       for(Preservation_Activities__c oActivities:trigger.old){                 
            setPreserveId.add(oActivities.Preservation_Object__c); 
     }
   }
    system.debug('@developer-->setPreserveId: '+setPreserveId);
    if(setPreserveId.size()>0){ 
        for(Preservation_Activities__c oActivities:[
            Select Preservation_Object__c,id,Name,Activity_Type__c,Activity_Date__c  
            from Preservation_Activities__c 
            where Preservation_Object__c In:setPreserveId and Activity_Date__c != null and isDeleted = FALSE
            order by Activity_Date__c desc limit 1]){
                Preservation_Object__c oPreserve = new Preservation_Object__c();
                oPreserve.Id = oActivities.Preservation_Object__c;
                oPreserve.Last_Activity_Type__c = oActivities.Activity_Type__c;
                listPreserve.add(oPreserve);
            }
        system.debug('@developer-->listPreserve: '+listPreserve); 
        if(listPreserve.size()>0){ 
            update listPreserve;
        }
    }
}
 You should make any other changes accordingly....

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

Best,
Nithesh