• cloudboom.ph
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies

I have a trigger that is counting child account records and rolling them up to the parent.  When I edit records individualy, I get the correct counts.  However, when I use the Data Loader, my aggregate results are far higher than they should be.  Can someone help me modify my trigger to make it work for bulk updates?

trigger AccountChildRollup2 on account (after update, after insert, after delete) {
    set<id> pid = new set<id>();
    list<account> childupdate = new list<account>();
    if (trigger.isinsert || trigger.isupdate){
        for (account a : trigger.new){
            pid.add(a.parentid);
        }
    }
    if (trigger.isdelete){
        for (account a : trigger.old){
            pid.add(a.parentid);
        }
    }
    aggregateresult children = [select count(id) from account where parentid != null and parentid in: pid];
    aggregateresult livechildren = [select count(id) from account where type = 'Live' and parentid in: pid];
    aggregateresult golivedate = [select min(go_live_date__c) from account where go_live_date__c != null and type = 'Live' and parentid in: pid];
    integer childcount = integer.valueof(children.get('expr0'));
    integer livechildcount = integer.valueof(livechildren.get('expr0'));
    date mingolivedate = date.valueof(golivedate.get('expr0'));
    
    map<id,account> pmap = new map<id,account>([select id, type, go_live_date__c, count_child_accounts__c, count_live_child_accounts__c from account where id in: pid]);
    
    if (trigger.isinsert || trigger.isupdate){
        for (account a : trigger.new){
            if (childcount > 0 && a.parentid != null && pmap.containsKey(a.parentid)){
                account p = pmap.get(a.parentid);
                p.count_child_accounts__c = childcount;
                p.count_live_child_accounts__c = livechildcount;
                p.this_is_a_parent_account__c = true;
                if (livechildcount > 0 && pmap.containsKey(a.parentid)){
                    p.type = 'Live';
                    p.go_live_date__c = mingolivedate;
                }
                p.validation_override__c = datetime.now();
                p.button_validate__c = datetime.now();
                pmap.put(p.id,p);
            }
        }
    }
    if (trigger.isdelete){
        for (account a : trigger.old){
            if (a.parentid != null && pmap.containsKey(a.parentid)){
                account p = pmap.get(a.parentid);
                p.count_child_accounts__c = childcount;
                p.count_live_child_accounts__c = livechildcount;
                p.this_is_a_parent_account__c = true;
                if (livechildcount > 0 && pmap.containsKey(a.parentid)){
                    p.type = 'Live';
                    p.go_live_date__c = mingolivedate;
                }
                p.validation_override__c = datetime.now();
                p.button_validate__c = datetime.now();
                pmap.put(p.id,p);
            }
        }
    }
    if(pmap != null)
    update pmap.values();
}

 

Hi all,

I am trying to develop a trigger, but it seems I keep hitting a Null Pointer Exception and cannot figure out why.  Anyone can help take a look?

 

trigger AccountChildRollup2 on account (before update, before insert) {
    set<id> pid = new set<id>();
    for (account child : trigger.new){
        pid.add(child.parent.id);
    }
    aggregateresult children = [select count(id) from account where parentid in: pid];
    aggregateresult livechildren = [select count(id) from account where type = 'Live' and parentid in: pid];
    integer childcount = integer.valueof(children.get('expr0'));
    integer livechildcount = integer.valueof(livechildren.get('expr0'));
    
    map<id,account> pmap = new map<id,account>([select id, type, go_live_date__c, count_child_accounts__c, count_live_child_accounts__c from account where id in: pid]);
    
    for (account child : trigger.new){
        account p = pmap.get(child.parentid);
        p.count_child_accounts__c = childcount;
        p.count_live_child_accounts__c = livechildcount;
        pmap.put(p.id,p);
    }
    update pmap.values();

}

Error:Apex trigger AccountChildRollup2 caused an unexpected exception, contact your administrator: AccountChildRollup2: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AccountChildRollup2: line 15, column 1 

Hi all,

I am trying to develop a trigger, but it seems I keep hitting a Null Pointer Exception and cannot figure out why.  Anyone can help take a look?

 

trigger AccountChildRollup2 on account (before update, before insert) {
    set<id> pid = new set<id>();
    for (account child : trigger.new){
        pid.add(child.parent.id);
    }
    aggregateresult children = [select count(id) from account where parentid in: pid];
    aggregateresult livechildren = [select count(id) from account where type = 'Live' and parentid in: pid];
    integer childcount = integer.valueof(children.get('expr0'));
    integer livechildcount = integer.valueof(livechildren.get('expr0'));
    
    map<id,account> pmap = new map<id,account>([select id, type, go_live_date__c, count_child_accounts__c, count_live_child_accounts__c from account where id in: pid]);
    
    for (account child : trigger.new){
        account p = pmap.get(child.parentid);
        p.count_child_accounts__c = childcount;
        p.count_live_child_accounts__c = livechildcount;
        pmap.put(p.id,p);
    }
    update pmap.values();

}

Error:Apex trigger AccountChildRollup2 caused an unexpected exception, contact your administrator: AccountChildRollup2: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AccountChildRollup2: line 15, column 1