I'm getting the following error when attempting to mass update accounts via Data Loader. The id throwing the error is the same in each instance. The Trigger (listed below) is meant to aggregate field ApexHours__c from child accounts, combine with the value of the parent and populate Total_Hours__c. The account that's triggering the error has a parent, while I've successfully updated those w/o parent accounts. So I'm assuming I'm referencing a null value somewhere but can't find it.

"TIParentRollUp: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00130000005o3WCAAY; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TIParentRollUp: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.TIParentRollUp: line 9, column 1: [] Trigger.TIParentRollUp: line 20, column 1"


trigger TIParentRollUp on Account (after insert, before update, after update, after delete, after undelete) {
if(Trigger.isBefore) {
    for(account a:trigger.new)
        if(a.ApexHours__c ==null)
            a.ApexHours__c =0;
            a.Total_Hours__c=a.ApexHours__c ;
    for(aggregateresult a:[select parentid,sum(ApexHours__c )sumb from account where ApexHours__c <>null and parentid in :trigger.new group by parentid])
        trigger.newmap.get((id)a.get('parentid')).Total_Hours__c += (decimal)a.get('sumb');
} else {
    set<id> parents = new set<id>();
        for(account a:trigger.old)
        for(account a:trigger.new)
    for(account[] aa:[select id from account where id in :parents])
       update aa;



1 Answer 1


The line:

trigger.newmap.get((id)a.get('parentid')).Total_Hours__c +=

is causing the issue. With the += operator the value of Total_Hours__c MUST NOT BE NULL.

To fix change line to this:

for(aggregateresult a:[select parentid,sum(ApexHours__c )sumb from account where ApexHours__c <>null and parentid in :trigger.new group by parentid]){
    if(trigger.newmap.get((id)a.get('parentid')).Total_Hours__c == null) trigger.newmap.get((id)a.get('parentid')).Total_Hours__c = 0;
    trigger.newmap.get((id)a.get('parentid')).Total_Hours__c += (decimal)a.get('sumb');
  • Tried to correct the line. Now error with: trigger.newmap.get((id)a.get('parentid')).Total_Hours__c += (decimal)a.get('sumb'); Variable does not exist: a
    – Ian Bury
    Jan 11, 2015 at 1:59
  • @IanBury - See updated answer.
    – Eric
    Jan 11, 2015 at 2:33
  • @IanBury - See updated answer - You needed to add the brackets around the statements since they are in a loop.
    – Eric
    Jan 11, 2015 at 2:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .