+ Start a Discussion

System.ListException: Duplicate id in list


I keep getting this error on one my triggers:

"System.ListException: Duplicate id in list:..."

I'm not sure where the issue is but below is the trigger:

trigger updateContactExactTarget on xtma_Individual_Email_Result__c (after insert) {

List <String> Code = new List <String> ();
    for (Integer i = 0; i < Trigger.new.size(); i++) {
        if(Trigger.new[i].Report_Name__c != null && Trigger.new[i].Contact__c != null) {
            Code.add(Trigger.new[i].Code2__c);       // List of the codes
    // get all the ContactExactTarget which have the same code as the code of IndividualEmailResult
    List <ContactExactTarget__c> CodeList = new List <ContactExactTarget__c> ([Select Id, Name, Code__c, Individual_Email_Result2__c from ContactExactTarget__c where Code__c in :Code and Individual_Email_Result2__c = null]);
List <ContactExactTarget__c> CETsToUpdate = new List <ContactExactTarget__c>();
    for(ContactExactTarget__c c : codeList) {
        for (Integer i = 0; i < Trigger.new.size(); i++) {
            if (c.code__c == Trigger.new[i].Code2__c) {
                c.Individual_Email_Result2__c = Trigger.new[i].Id; // Populating the Individual Email Result field
    if(CETsToUpdate != null || CETsToUpdate.size() >0)
       update CETsToUpdate;                   // Update the list of ContactExactTarget

Can someone help me fix this trigger?
Niket SFNiket SF
This will be temp solutions. 

 before doing DML operation. Please fire a loop on CETsToUpdate to create a Map<Id,ContactExactTarget__c> MapConExact so there will not be duplicate Ids

and do DML operation on 

update MapConExact.Values();