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
cloudboom.phcloudboom.ph 

Bulk Update and Aggregate Results

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