You need to sign in to do that
Don't have an account?
Bulkify Roll Up Summary Trigger
Hello,
I was wondering if someone would be able to help me bulifying the trigger below. As of now, this trigger counts all of the calls made in one month on a particular account made by a particular system profile.
I just was given the requirements that I need to do the same thing but for another profile, AL - AM Rep and write to a different field, AM_Activity_Count__c. I know I could just duplicate this trigger, change the name in the soql, and then save it. But I know there has to be a way that I can bulkify this. I've been messing around trying to get this to work and I've been unable to get it to work so far. Here't what I have originally:
trigger Activity_Count on Task (after insert, after update, after delete)
{
List<Id> taskIds = new List<Id>();
List<Id> accIds = new List<Id>();
Map<Id,Account> accMap = new Map<Id,Account>();
List<Account> updateList = new List<Account>(); // edited
String accPrefix = Account.sObjectType.getDescribe().getKeyPrefix();
for (Task t : Trigger.isDelete ? Trigger.old : Trigger.new)
{
taskIds.add(t.Id);
String str = t.whatId;
if(str != null && str.startsWith(accPrefix)) // Task belongs to Account
{
accIds.add(t.whatId);
}
}
if(accIds.size() > 0)
{
for(Account ac : [SELECT Id, Activity_Count__c FROM Account WHERE Id in :accIds])
{
accMap.put(ac.Id,ac);
}
for(AggregateResult ar : [SELECT Count(Id) c, WhatId w, Owner.Profile.Name
FROM Task
WHERE whatId IN :accIds
AND Subject Like 'Call%'
AND CreatedDate = This_Month
AND Owner.Profile.Name = 'AL - AS Rep'
GROUP BY WhatId, Owner.Profile.Name])
{
Account acc = new Account();
string str = string.valueOf(ar.get('w'));
acc = accMap.get(str); // Edited //
acc.Activity_Count__c = Integer.valueOf(ar.get('c')); // Edited
updateList.add(acc);
}
}
if(updateList.size() > 0)
{
update updateList;
}
}
try this.
All Answers
try this.
Thanks Naidu for your help!
I just tested it and doesn't seem to like the line:
updateList = acc.values();
Here's the error it gives:
Updated the code. try it.
No dice. It's still failing on the same line with the same error. :( Do the curly brackets starting before the line Account acc = new Account(); need to be removed?
try now. I forgot to update the code where i am bringing the values from the accMap.
Perfect! Thanks Naidu! :)
trigger AddivantPriceRecordItemTrigger on Price_Record_Item__c (after insert, after update,after delete, after undelete) {
//Variables
set<id> parentRecordIDset = new set<id>();
list<Price_Record__c> parentRecordlist = new list<Price_Record__c>();
list<Price_Record_Item__c> childRecordList = new list<Price_Record_Item__c>();
//Get the parent record id from child record
for(Price_Record_Item__c childrecord : ((!trigger.isdelete)?trigger.new:trigger.old)){
parentRecordIDset.add(childrecord.Price_Record__c);
}
//Get parent record list
parentRecordlist = [SELECT id
,Items_Status__c
FROM Price_Record__c
WHERE id in :parentRecordIDset];
//get child record list
childRecordList = [SELECT id
,Status__c
,Price_Record__c
FROM Price_Record_Item__c
WHERE Price_Record__c in :parentRecordIDset];
//Apply the logic in this loop
for(Price_Record__c parentrecord : parentRecordlist){
//Reset the rollup fields for parent record
parentrecord.Item_Status__c = '';
for(Price_Record_Item__c childrecord : childRecordList){
if(parentrecord.id == childrecord.Price_Record__c){
//Put your roll up logic here
parentrecord.Item_Status__c = parentrecord.Item_Status__c + childrecord.Status__c + ',';
}
}
}
//Update parent record
update parentRecordlist;
}//EOF