I have a trigger that counts the number of child records by type and adds that to the parent. Each type has a field on the parent where the count is added. The update part of the trigger works fine during bulk uploads of child records. The delete works when child records are deleted one at a time. The problem is that the trigger times out when I try to delete more than 1.
I get the following error.
ERROR: System.DmlException: Delete failed. First exception on row 0 with ! id a0s1U000000v1SyQAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ! CMRules.pickListCount: execution of AfterDelete ! ! caused by: System.ListException: Duplicate id in list: a1N1U000001kDV6UAM
Any help would be appreciated! Thanks!
if(trigger.isDelete){
Map<Id,List<wbsendit__Campaign_Activity__c>> mapsentList = new
Map<Id,List<wbsendit__Campaign_Activity__c>>();
List<Id> summaryList = new List<Id>();
Set<id> summarySet = new set<Id>();
list<wbsendit__Campaign_Activity__c> summaryActivityList = new
list<wbsendit__Campaign_Activity__c>();
for(wbsendit__Campaign_Activity__c tc : trigger.Old){
if(tc.Email_Tracking_Summary__c!=null){
wbsendit__Campaign_Activity__c tempRecords = new
wbsendit__Campaign_Activity__c(id = tc.ID, Email_Tracking_Summary__c =
tc.Email_Tracking_Summary__c, wbsendit__Activity__c =
tc.wbsendit__Activity__c);
summaryActivityList.add(tempRecords);
summaryList.add(tc.Email_Tracking_Summary__c);
summarySet.add(tc.Email_Tracking_Summary__c);
mapsentList.put(tc.Id,summaryActivityList);
}
}
List<Email_Tracking_Summary__c> sentList = new
List<Email_Tracking_Summary__c>();
List<Email_Tracking_Summary__c> openList = new
List<Email_Tracking_Summary__c>();
List<Email_Tracking_Summary__c> clickList = new
List<Email_Tracking_Summary__c>();
List<Email_Tracking_Summary__c> unsubList = new
List<Email_Tracking_Summary__c>();
List<Email_Tracking_Summary__c> bounceList = new
List<Email_Tracking_Summary__c>();
for(Email_Tracking_Summary__c c : [SELECT id, Sent__c, Opened__c, Clicked__c,
Unsubscribed__c, Bounced__c FROM Email_Tracking_Summary__c WHERE id in:
summarySet]){
for(List<wbsendit__Campaign_Activity__c> idList : mapsentList.values()){
for(wbsendit__Campaign_Activity__c idVal : idList){
// begin populating lists to update counts
// list for open sent
if(c.Id == idVal.Email_Tracking_Summary__c &&
idval.wbsendit__Activity__c == 'Sent'){
c.Sent__c = c.Sent__c - 1;
sentList.add(c);
}
// list for open deletes
else if(c.Id == idVal.Email_Tracking_Summary__c &&
idval.wbsendit__Activity__c == 'Opened'){
c.Opened__c = c.Opened__c - 1;
openList.add(c);
}
// List for clicks
else if(c.Id == idVal.Email_Tracking_Summary__c &&
idval.wbsendit__Activity__c == 'Clicked'){
c.Clicked__c = c.Clicked__c - 1;
clickList.add(c);
}
// List for unsub list
else if(c.Id == idVal.Email_Tracking_Summary__c &&
idval.wbsendit__Activity__c == 'Unsubscribed'){
c.Unsubscribed__c = c.Unsubscribed__c - 1;
unsubList.add(c);
}
// List for unsub deletes
else if(c.Id == idVal.Email_Tracking_Summary__c &&
idval.wbsendit__Activity__c == 'Unsubscribed'){
c.Unsubscribed__c = c.Unsubscribed__c - 1;
unsubList.add(c);
}
// List for bounced deletes
else if(c.Id == idVal.Email_Tracking_Summary__c &&
idval.wbsendit__Activity__c == 'Bounced'){
c.Bounced__c = c.Bounced__c - 1;
bounceList.add(c);
}
}
}
}
update sentList;
update openList;
update clickList;
update unsubList;
update bounceList;
}