+ Start a Discussion
jonpilarski1.3914448382645588E12jonpilarski1.3914448382645588E12 

System.ListException: Duplicate id in list

Hello. Very new to APEX so be kind :)

I have a rather simple trigger which evaluates Applications (children of Contact) Depending on some specific logic I want to update the Contact. I will have multuple Applciations for the same Contact so I put them in a Set. The last one added should be the only one persisted as I am adding to a Set. However, I get System.ListException when here:

List<Contact> toUpdate = new List<Contact>();
        toUpdate.addAll(student);              // add SET to LIST
     
        update toUpdate;                          //this operation throws: System.ListException: Duplicate id in list

Here is whole trigger.

Thanks,
Jon

trigger studentStage on TargetX_SRMb__Application__c (before insert, before update) {

Set<Contact> student = new Set<Contact>();

for (TargetX_SRMb__Application__c app : trigger.new) {

  if ( app.TargetX_SRMb__Level__c != null && app.TargetX_SRMb__Level__c.equals('Undergraduate')){
     try{
   
      if (      String.IsBlank(app.TargetX_SRMb__Application_Decision__c) ||
                app.TargetX_SRMb__Application_Decision__c.length() == 0 ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled Implied') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Waitlist') ||              
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled before offer') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Denied') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Conditional status') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Accepted to ELA program') ) {
                         
         student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Applicant' ));
  
      }else if (app.TargetX_SRMb__Application_Decision__c.equals('Accepted') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled after order') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Deferral') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Accepted to EOP program') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Waitlist admit'))  {
         student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Admitted' ));
    
      }else if (app.TargetX_SRMb__Application_Decision__c.equals('Confirmed') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Confirmed 2') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled after confirmed'))  {
         student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Confirmed' )); 
           
      }else if (app.TargetX_SRMb__Contact__r.TargetX_SRMb__Anticipated_Start_Term__c == null) {
    
   
      }
    
      }catch(NullpointerException ne){
         student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Applicant' ));
       //   studentList.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Applicant' ));
      }catch(ListException le) {
      }
   

    }else if (app.TargetX_SRMb__Level__c.equals('Graduate') || app.TargetX_SRMb__Level__c.equals('Doctoral') ){

System.debug('student is graduate' + app.TargetX_SRMb__Level__c+ ' '+ app.TargetX_SRMb__Application_Decision__c);


 
     try{
   

   
       if (     String.IsBlank(app.TargetX_SRMb__Application_Decision__c) ||
                app.TargetX_SRMb__Application_Decision__c.length() == 0 ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled Implied') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Denied') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Conditional status') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Accepted ELA program')
                 ) {


System.debug('adding to list '+app.TargetX_SRMb__Application_Decision__c);
   
         student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Applicant' )); 

      }else if (app.TargetX_SRMb__Application_Decision__c.equals('Accepted') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled after offer') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Conditional status') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Deferral') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Accepted EOP offer') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Waitlist admit') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Accepted EOP program')  )  {
         student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Admitted' ));
      
        System.debug('adding to list '+app.TargetX_SRMb__Application_Decision__c);
     }else if (app.TargetX_SRMb__Application_Decision__c.equals('Confirmed') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Confirmed 2') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled after confirmed'))  {
         student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Confirmed' ));
  
      }else if (app.TargetX_SRMb__Contact__r.TargetX_SRMb__Anticipated_Start_Term__c == null){
      //   student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Inquiry' ));
      }  
   
 
   
     }catch(NullpointerException ne){
        student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Applicant' ));

     }catch(ListException le) {
     
     }
  
   
     }


    
    }// end for loop
  

    if(!student.isEmpty()){
  
    try{

    
        List<Contact> toUpdate = new List<Contact>();
        toUpdate.addAll(student);              // add set to list
          System.debug('update contacts');
        update toUpdate;                          //this operation throws: System.ListException: Duplicate id in list
          System.debug('after update');

    }catch(ListException le) {
      System.debug('error : '+le);
    }
    }




}
Coco_SdyneyCoco_Sdyney
Hi Jon, try below:

trigger studentStage on TargetX_SRMb__Application__c (before insert, before update) {

Set<Contact> student = new Set<Contact>();
Set<Id> studentId = new Set<Id>();

for (TargetX_SRMb__Application__c app : trigger.new) {

  if ( app.TargetX_SRMb__Level__c != null && app.TargetX_SRMb__Level__c.equals('Undergraduate')){
     try{
  
      if (      String.IsBlank(app.TargetX_SRMb__Application_Decision__c) ||
                app.TargetX_SRMb__Application_Decision__c.length() == 0 ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled Implied') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Waitlist') ||             
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled before offer') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Denied') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Conditional status') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Accepted to ELA program') ) {
               
                if(!studentId.contains(app.TargetX_SRMb__Contact__c))
                {
                        studentId.add(studentId);
                        student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Applicant' ));
                }
       
         
 
      }else if (app.TargetX_SRMb__Application_Decision__c.equals('Accepted') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled after order') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Deferral') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Accepted to EOP program') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Waitlist admit'))  {
                if(!studentId.contains(app.TargetX_SRMb__Contact__c))
                {
                        studentId.add(studentId);
                        student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Admitted' ));
                }


   
      }else if (app.TargetX_SRMb__Application_Decision__c.equals('Confirmed') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Confirmed 2') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled after confirmed'))  {
               if(!studentId.contains(app.TargetX_SRMb__Contact__c))
                {
                        studentId.add(studentId);
                        student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Confirmed' ));
                }        


          
      }else if (app.TargetX_SRMb__Contact__r.TargetX_SRMb__Anticipated_Start_Term__c == null) {
   
  
      }
   
      }catch(NullpointerException ne){
               if(!studentId.contains(app.TargetX_SRMb__Contact__c))
                {
                        studentId.add(studentId);
                        student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Applicant' ));
                }        


       //   studentList.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Applicant' ));
      }catch(ListException le) {
      }
  

    }else if (app.TargetX_SRMb__Level__c.equals('Graduate') || app.TargetX_SRMb__Level__c.equals('Doctoral') ){

System.debug('student is graduate' + app.TargetX_SRMb__Level__c+ ' '+ app.TargetX_SRMb__Application_Decision__c);



     try{
  

  
       if (     String.IsBlank(app.TargetX_SRMb__Application_Decision__c) ||
                app.TargetX_SRMb__Application_Decision__c.length() == 0 ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled Implied') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Denied') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Conditional status') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Accepted ELA program')
                 ) {


System.debug('adding to list '+app.TargetX_SRMb__Application_Decision__c);
                if(!studentId.contains(app.TargetX_SRMb__Contact__c))
                {
                        studentId.add(studentId);
                        student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Applicant' ));
                }

        

      }else if (app.TargetX_SRMb__Application_Decision__c.equals('Accepted') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled after offer') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Conditional status') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Deferral') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Accepted EOP offer') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Waitlist admit') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Accepted EOP program')  )  {
                if(!studentId.contains(app.TargetX_SRMb__Contact__c))
                {
                        studentId.add(studentId);
                        student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Admitted' ));
                }         


     
        System.debug('adding to list '+app.TargetX_SRMb__Application_Decision__c);
     }else if (app.TargetX_SRMb__Application_Decision__c.equals('Confirmed') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Confirmed 2') ||
                app.TargetX_SRMb__Application_Decision__c.equals('Cancelled after confirmed'))  {
                if(!studentId.contains(app.TargetX_SRMb__Contact__c))
                {
                        studentId.add(studentId);
                        student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Confirmed' ));
                }         


 
      }else if (app.TargetX_SRMb__Contact__r.TargetX_SRMb__Anticipated_Start_Term__c == null){
      //   student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Inquiry' ));
      } 
  

  
     }catch(NullpointerException ne){
               if(!studentId.contains(app.TargetX_SRMb__Contact__c))
                {
                        studentId.add(studentId);
                        student.add(new Contact(Id = app.TargetX_SRMb__Contact__c, TargetX_SRMb__Status__c = 'Applicant' ));
                }

        

     }catch(ListException le) {
    
     }
 
  
     }


   
    }// end for loop
 

    if(!student.isEmpty()){
 
    try{

   
        List<Contact> toUpdate = new List<Contact>();
        toUpdate.addAll(student);              // add set to list
          System.debug('update contacts');
        update toUpdate;                          //this operation throws: System.ListException: Duplicate id in list
          System.debug('after update');

    }catch(ListException le) {
      System.debug('error : '+le);
    }
    }




}
Sagarika RoutSagarika Rout
Instead of updating  the list , Use Upsert . If there is a new record it will insert and if there is old record , it will update.
jonpilarski1.3914448382645588E12jonpilarski1.3914448382645588E12
Thank You Coco_Sdyney. Worked perfectly. Best