You need to sign in to do that
Don't have an account?
jaw999
Bulkify Roll Up Summary Trigger
I just need to do a trigger version of a Roll up summary. Made it easily for single use but mass Apex updates overload. Here's my un-bulked code below. Any help would be apprecieated.
Assigned_Team__c is a related object to Account that I want to count.
trigger CountCov on Account (before insert, before update) { Account [] a = Trigger.new; String aid = null; Aid = a[0].id; Integer i = [select count() from Assigned_Team__c where Account__c = :aid and Name != 'None']; a[0].Coverage_count__c = i; }
try this.
All Answers
for(AggregateResult agg : [Select Count() cCount, Account__c accId from Assigned_Team__c where Account__c IN: Trigger.newMap.keySet() group by Assigned_Team__c]) {
accountsList.add(new Account(Id = Id.valueOf(String.valueOf(agg.get(accId))), Coverage_count__c = agg.get('cCount')));
}
update accountList;
thanks - but not quite compiling - what am i missing?
Error: Compile Error: unexpected token: Account__c at line 6 column 47
Try below code
thanks! but i ran into an error when inserting new accounts:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CountCov caused an unexpected exception, contact your administrator: CountCov: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CountCov: line 5, column 1
Add these line below Map declaration at line 4(below).
set<Id> accountIds = new Set<Id>();
for(account a: trigger.new)
accountIds.add(a.Id);
Was that an insert fix?
try below modified mode
yes ,it will work for both before insert and update
To show zero on account for no assignments remove this line
if(acctToassigntsMap.get(a.id) != 0)
here is what i have - it won't show a zero and it still counts a team named 'NONE' : thanks again
add where cluse in soql query where Name != null
Odd. I had an Assigned_Team__c called NONE - or so I thought. Maybe it has a space after so it was ebign counted. Anyway, looks good, thanks.
It seems to work only if i also comment out this other line (work in terms of updating when it should be a zero, though it still puts in a blank instead of a zero)
does that make sense?
try this.