Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Bulk Update and Aggregate Results
turn on suggestions
Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
0
Bulk Update and Aggregate Results
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-10-2012 05:04 AM
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();
}

