function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GMASJGMASJ 

System.ListException: Duplicate id in list in Code

Hi,

  I am getting error below error while creating a new contact it is not happening while updating the contact. 
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger ContactTrigger caused an unexpected exception, contact your administrator: ContactTrigger: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 001W000000amehmIAA: ()".
 Below is the code it is working perfect for updating the contact during creating a new contact i am getting duplicate list Please suggest me how to 
 modify the code I have highted the code which is giving me the error. 

 Please suggest me how to remove duplicates 
Trigger
========

if (Trigger.isAfter) {
  if(trigger.isInsert){
     ContactCertificateRollupHandlerNew1.ProcessInsert(Trigger.new);
  } else if(trigger.isUpdate) {
    ContactCertificateRollupHandlerNew1.ProcessInsert(Trigger.new); 
  }
   
   
Helper Class
=============

public class ContactCertificateRollupHandlerNew1{
 
Public static Map<id,integer> NSEmap1;
Public static Map<id,integer> NSEmap2;

Public static Integer NSEint1 = 0;
Public static Integer NSEint2 = 0;

Public static List<account> NSE1_Update = new List<account>();
Public static List<account> NSE2_Update = new List<account>(); 
 
Public static map<id,account> NSE1_Update_Map = new map<id,account>();
Public static map<id,account> NSE2_Update_Map = new map<id,account>();
 
 public static void ProcessInsert (List<Contact> newCntLst){
   list<id> actidSet = new list<id>();
       
     for(Contact cont : newCntLst){
       actidSet.add(cont.AccountId);                                 
      }  
      
     ProcessUpdate(actidSet[0]); 
     //GetLastAccounts(actidSet[0]);   
 }
 
 
 public static void ProcessUpdate (Id AccountID){
     
    GetLastAccounts(AccountID);    
  }
     
     
  public static void GetLastAccounts (Id AccountID){    
   
    set<id> GetAllActID = new set<id>();
    
    GetAllActID = AccountHierarchyUtil.ChildToParnet(AccountID);
    GetAllActID = AccountHierarchyUtil.ParnetToChild(AccountID);
    
    List<Account> LastAct = [SELECT Id, name FROM Account where id in :GetAllActID];
  
    Account LastAccount  = LastAct[LastAct.size() - 1];   
    
    System.debug('LastAccount ID ' + LastAccount.id);
    System.debug('LastAccount Name ' + LastAccount.name);
    
     accountRollupUpdate(LastAccount.id);
   }
   
  
   public static void accountRollupUpdate(Id PAccountID) {
        
        Id accountId = PAccountID;
        
        NSEmap1 = new Map<id,integer>();
        NSEmap2 = new Map<id,integer>();

        ChildToParnet(accountId);


        if(NSEmap1.size()>0){
    
            For(Id i : NSEmap1.keyset()){        
                Account ains = new Account(); 
                ains.id = i;
        
                system.debug('Account' + i);       
                system.debug(NSEmap1.get(i));
        
                ains.NSE_1_Status__c = NSEmap1.get(i);
                   NSE1_Update.add(ains);                      
            }
        }


       if(NSEmap2.size()>0){
    
           For(Id i : NSEmap2.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap2.get(i));
        
              ains.NSE_2_Status__c = NSEmap2.get(i);        
                NSE2_Update.add(ains);
               
        
    }
    
}

        if(NSE1_Update.size()>0)   //These are line where the i am getting duplicate error while inserting
          
          update NSE1_Update;


        if(NSE2_Update.size()>0)
        
          update NSE2_Update;  
            
          /* map<id,account> nse1map = new map<id,account>();
            nse1map.putall(NSE1_Update);
        
            if(nse1map.size()>0){
            update nse1map.values();         
            }
            
            map<id,account> nse2map = new map<id,account>();
            nse2map.putall(NSE2_Update);
        
            if(nse2map.size()>0){
            update nse2map.values();
            }    */        
            
        
   }



public static void ChildToParnet (Id ChildId){
     
    For(Account acc : [select id,name, parentid, (Select id,NSE_1__c,NSE_2__c from contacts)from account where id =: ChildId]){
        
        For(Contact c : acc.contacts ){
            
            if(c.NSE_1__c == true)               
                 NSEint1 = NSEint1 +1;                
                 
            
            if(c.NSE_2__c == true)
                NSEint2 = NSEint2 + 1;                          
                            
            
        }
           NSEmap1.put(acc.id,NSEint1);
           NSEmap2.put(acc.id,NSEint2);  
        
        if(acc.parentid != null)            
           childToParnet(acc.parentid);
        
     }
  }
  
  }
  
Helper Class
=============

public class AccountHierarchyUtil{
    
    
    public static set<id> ChildToParnet (Id PAccountID){
        
        set<id> setactid = new set<id>();
        
        Id accountId = PAccountID;
        
        Account[] allparents = new Account[] {};
            
            Set<Id> parentIds = new Set<Id>{accountId};
                
                Account[] parent;
        
        do {
            
            parent = [select Id,ParentId, Name from Account where Id in :parentIds];
            
            allparents.addAll(parent);
            
            parentIds.clear();
            
            for (Account par : parent) 
                
                parentIds.add(par.ParentId);
            
        } while (parent.size() > 0);
        
        list<Account> Act = [select id, name from account where id in :allparents];
        
        for(Account A : Act){
            
            system.debug('Parent Accounts ' + a.name);  
            setactid.add(a.id);
            
        }      
        
        return setactid;
        
    }
    
    public static set<id> ParnetToChild (Id PAccountID){
        
        set<id> setactid = new set<id>();
        
        Id accountId = PAccountID;
        
        Account[] allChildren = new Account[] {};
            
            Set<Id> parentIds = new Set<Id>{accountId};
                
                Account[] children;
        
        do {
            
            children = [select Id, Name from Account where ParentId in :parentIds];
            
            allChildren.addAll(children);
            
            parentIds.clear();
            
            for (Account child : children) 
                
                parentIds.add(child.Id);
            
        } while (children.size() > 0);
        
        list<Account> Act = [select id, name from account where id in :allChildren];
        
        for(Account A : Act){
            
            system.debug('Child Accounts ' + a.name);  
            setactid.add(a.id);      
            
        }
        return setactid;
    }  
    
    
}

Thanks
Sudhir
Best Answer chosen by GMASJ
RD@SFRD@SF
Hi Sudhir,

Tracing the process flow would tell you how to avoid the duplicates but if you want a quick fix. Iterate the final udpate list through a set and update it. Set by its own property removes the duplicates

Hope it helps
RD