1

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;
}
  • Where does this come from in the error message: TSG_CMRules.pickListCount - could that point you to some code that interferes with your trigger? Maybe you could use a couple of debug statements to locate where your error occurs more precisely. – user40501 Aug 23 at 6:20
2

Your triple nesting of loops is likely to consume a large amount of CPU time needlessly, and you may want to look at how to simplify the structure. But typically you can get around this type of error by simply using a Map rather than a List.

Map<Id, SObject> records = new Map<Id, SObject>();
// put record in by Id as many times as you like
update records.values();
  • 1
    Thanks @AdrianLarson. Using a map worked, but as you mentioned the nested loops caused a time out when testing the deletion of a large number of records. I will have to see how to simplify the structure as you suggested. Thanks for the help! – DGOU182 Aug 23 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.