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
Ivan WinzerIvan Winzer 

duplicate id in list

 

So after all that work when i try to do an update to the records in my address object im now getting a duplicate error: 

 

system.listexception: duplicate id in list: 003400000cWe1BAAS trigger.mainshiptoaddressTrigger: line 29, column 1

 

Seems it does not like my update statement....
trigger mainShipToAddessTrigger on ShipTo_Address__c (before insert, before update) {
   List<ShipTo_Address__c> slist = new List<ShipTo_Address__c>();
   List<Contact> contactsToUpdate = new List<Contact>();
   for ( ShipTo_Address__c s : trigger.new ) { 
       if ( s.Primary_Billing_Address__c == true) {
           Contact c = New Contact(Id = s.Contact__c);
           system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c);

           c.MailingStreet = s.Address__c;
           c.MailingCity = s.City__c;
           c.MailingState = s.State__c;
           c.MailingCountry = s.Country__c;
           c.MailingPostalCode = s.ZIP__c;
     contactsToUpdate.add(c);
   //        update c;             
       }
       if ( s.Default_Shipping_Address__c == true) {
           Contact c = New Contact(Id = s.Contact__c);
           system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c);

           c.OtherStreet = s.Address__c;
           c.OtherCity = s.City__c;
           c.OtherState = s.State__c;
           c.OtherCountry = s.Country__c;
           c.OtherPostalCode = s.ZIP__c;
           contactsToUpdate.add(c);
  //         update c; 
       }
      update contactsToUpdate;
   }  
}


Raghu Reddy 14Raghu Reddy 14
Hi Ivan,

The issue is you are adding same contact two times to the list and trying to update. 

Check if both Primary_Billing_Address__c and  Default_Shipping_Address__c values are true also. 

Here is the updated code.

trigger mainShipToAddessTrigger on ShipTo_Address__c (before insert, before update) {
   List<ShipTo_Address__c> slist = new List<ShipTo_Address__c>();
   List<Contact> contactsToUpdate = new List<Contact>();
   for ( ShipTo_Address__c s : trigger.new ) {
       if ( s.Primary_Billing_Address__c == true  &&  s.Default_Shipping_Address__c == true) {
           Contact c = New Contact(Id = s.Contact__c);
           c.MailingStreet = s.Address__c;
           c.MailingCity = s.City__c;
           c.MailingState = s.State__c;
           c.MailingCountry = s.Country__c;
           c.MailingPostalCode = s.ZIP__c;
           c.OtherStreet = s.Address__c;
           c.OtherCity = s.City__c;
           c.OtherState = s.State__c;
           c.OtherCountry = s.Country__c;
           c.OtherPostalCode = s.ZIP__c;
           contactsToUpdate.add(c);
       } else if (s.Primary_Billing_Address__c == true &&  s.Default_Shipping_Address__c == false)
       {
          Contact c = New Contact(Id = s.Contact__c);
           c.MailingStreet = s.Address__c;
           c.MailingCity = s.City__c;
           c.MailingState = s.State__c;
           c.MailingCountry = s.Country__c;
           c.MailingPostalCode = s.ZIP__c;
           contactsToUpdate.add(c);
     } else if (s.Primary_Billing_Address__c == false &&  s.Default_Shipping_Address__c == true)
       {
         Contact c = New Contact(Id = s.Contact__c);
         c.OtherStreet = s.Address__c;
         c.OtherCity = s.City__c;
         c.OtherState = s.State__c;
         c.OtherCountry = s.Country__c;
         c.OtherPostalCode = s.ZIP__c;
         contactsToUpdate.add(c);
       }
   } 
  update contactsToUpdate;
}

 
/Raghu




David "w00t!" LiuDavid "w00t!" Liu
You can also use a Set instead of a List, which will automatically dedupe for you!

Just make sure to change the Set back to a List before updating