+ Start a Discussion
SoundarSoundar 

Field is not writable on trigger

I got a below Error when i try to update account address (Billing Address) to Contact Address (Mailing Address)

************************************************************************************
 
trigger ContactMapToAccount on Contact (after Insert ) {
      List<Contact> lsCon = New List<Contact>();
    System.debug('Starting ++');
    //Set Of Id In Contact Code & Id
    Set<Id> conId = New Set<Id>();
    Set<String> conCode = New Set<String>();
    
    for(Contact c : Trigger.New){
         if(c.Account_Code__c != Null){
        conId.add(c.Id);
        conCode.add(c.Account_Code__c);
         } 
    }
    if(conCode.size() > 0)  {    
     System.debug('ContactId : ' + conId);
    
    List<Contact> lstCont = [Select id, Account_Code__c, name, MailingAddress, OtherAddress from Contact Where ID IN:conId];
    System.debug('Contact : ' + lstCont);
    
    List<Account> lstAcc  =[Select id,Account_Code__c, name,Phone,BillingAddress,ShippingAddress from Account Where Account_Code__c IN:conCode];
    System.debug('Account : ' + lstAcc);
    
    Map<String,Account> acMap = new Map<String,Account>();
    for(Account a : lstAcc){
        acMap.put(a.Account_Code__c, a);
    }
    
   /* Map<Id,Contact> conMap =  New Map<Id,Contact>();
    for(Contact c : lstCont){
        conMap.put(c.Id, c);
    }*/
   
    for(Contact c : lstCont){
        if(c.Account_Code__c != Null){
            Contact cont = New Contact(id = c.id);
        cont.AccountId = acMap.get(c.Account_Code__c).id;
        cont.phone = acMap.get(c.Account_Code__c).Phone;
        cont.Fax = acMap.get(c.Account_Code__c).Fax;   
        cont.MailingAddress = acMap.get(c.Account_Code__c).BillingAddress;     // Error Line
        lsCon.add(cont);    
    } 
        
   }
    if(lsCon.size() > 0){
        
        update lsCon;
    }
  }  
}

 
Best Answer chosen by Soundar
SoundarSoundar
Dear Friends,

It's Too Easy and Smiply (though i am new to the development ),  So only i having more doubts(Like too Small doubts) while development.

BillingAddress,ShibbingAddress,otherAddress,etc... is comprising a BillingStreet , BillingCity, BillingState,BillingCountry,BillingStateCode ... So we need to iterate by seperate fields ..



 
trigger ContactMapToAccount on Contact (after Insert ) {
      List<Contact> lsCon = New List<Contact>();
    System.debug('Starting ++');
    //Set Of Id In Contact Code & Id
    Set<Id> conId = New Set<Id>();
    Set<String> conCode = New Set<String>();
    
    for(Contact c : Trigger.New){
         if(c.Account_Code__c != Null){
        conId.add(c.Id);
        conCode.add(c.Account_Code__c);
         } 
    }
    if(conCode.size() > 0)  {    
     System.debug('ContactId : ' + conId);
    
    List<Contact> lstCont = [Select id, Account_Code__c, name, MailingAddress, OtherAddress from Contact Where ID IN:conId];
    System.debug('Contact : ' + lstCont);
    
    List<Account> lstAcc  =[Select id,Account_Code__c, name,Phone,BillingAddress,ShippingAddress from Account Where Account_Code__c IN:conCode];
    System.debug('Account : ' + lstAcc);
    
    Map<String,Account> acMap = new Map<String,Account>();
    for(Account a : lstAcc){
        acMap.put(a.Account_Code__c, a);
    }
    
   /* Map<Id,Contact> conMap =  New Map<Id,Contact>();
    for(Contact c : lstCont){
        conMap.put(c.Id, c);
    }*/
   
    for(Contact c : lstCont){
        if(c.Account_Code__c != Null){
            Contact cont = New Contact(id = c.id);
        cont.AccountId = acMap.get(c.Account_Code__c).id;
        cont.phone = acMap.get(c.Account_Code__c).Phone;
        cont.Fax = acMap.get(c.Account_Code__c).Fax;   
        cont.MailingCity = acMap.get(c.Account_Code__c).BillingCity;  *******
        cont.MailingState = acMap.get(c.Account_Code__c).BillingState;  *******
        cont.MailingCountry = acMap.get(c.Account_Code__c).BillingCountry;  ****
        cont.MailingPostalCode = acMap.get(c.Account_Code__c).BillingPostalCode;  ****
        lsCon.add(cont);    
    } 
        
   }
    if(lsCon.size() > 0){
        
        update lsCon;
    }
  }  
}

Regards,

Soundar Raj
+91-7418425418​