+ Start a Discussion
Abhishek PurohitAbhishek Purohit 

I am getting this error while using the Data Loader As FATAL ERROR System.Exception duplicate id in List.There is an error occuring in updateField Kindly help

trigger ShippingBillingAddress on Account (before insert,before update,after insert,after update) {
 if(Trigger.isBefore)
    {
     if(Trigger.isInsert || Trigger.isUpdate)
          {
            ShippingAddress.shipBillAddress(Trigger.New);
          }
     } 

 
  if(Trigger.isAfter)
   {
      if(Trigger.isInsert)
        {
          ShippingAddress.insertRecord(Trigger.New);
        }
      if(Trigger.isUpdate)
        {
           ShippingAddress.updateField(Trigger.New);
          
        
        
        
        }
    
   }
    
}



This is my Helper Class

public class ShippingAddress{
 public static void shipBillAddress(List<Account> accountObject){
 for(Account account:accountObject){
  account.ShippingStreet=account.BillingStreet;
  account.ShippingCountry=account.BillingCountry;
  account.ShippingCity=account.BillingCity;
  account.ShippingState=account.BillingState;
  } 
 }

 public static void updateField(List<Account> accountObject){
 system.debug('Account List'+accountObject);
     Set<Id> setOfAccountIds=new Set<Id>();
       for(Account account:accountObject)
           {
             setOfAccountIds.add(account.id);
           } 
 
     List<Contact> listOfContact=new List<Contact>([Select Id,AccountId FROM Contact WHERE AccountId IN:setOfAccountIds]);
            List<Contact> listOfUpdatedContact=new List<Contact>();
             
             for(Account accountOfObject:accountObject)
             {
              for(Contact contactOfObject:listOfContact)
               {
                contactOfObject.Phone=accountOfObject.Phone;
             
                listOfUpdatedContact.add(contactOfObject);
               }
             } 
       update listOfUpdatedContact;

 }
 
   public static void insertRecord(List<Account> accountObject){
     List<Contact> contactList=new List<Contact>();
    
    for(Account account:accountObject)
     {
         Contact contactObject = new Contact();
         contactObject.accountId=account.Id;
         contactObject.LastName=account.name;
         contactObject.Phone=account.Phone;
         
         contactList.add(contactObject);
     } 
    insert contactList;
       
   }
}


 
Alain CabonAlain Cabon

Hello, 
 
// for(Account accountOfObject:accountObject)
// {
          for(Contact contactOfObject:listOfContact)
           {    
                 contactOfObject.Phone =  contactOfObject.account.Phone;
 
select id, account.phone
from contact

Regards
Alain