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
HARISH S 11HARISH S 11 

trigger on contact record to fetch the account address

I have the the below code which works very well if I insert a new record, but if I add after update in the trigger it is not working and throws some organizational error. Could somebody please let me know what needs to be done if I need to update the existing records in the org?

trigger updatecontact1 on  Contact (after insert){
    List<contact> lstConUpdate = new List<Contact>();
    set<Id> sAccId = new set<Id>();
    for(Contact con: trigger.new){
        sAccId.add(con.AccountId);
    }
List<Account> lstAccount = [select id, Address_1__c, Address_2__c,City__c,Country__c, (select id,Address_1__c, Address_2__c,City__c,Country__c from contacts) from account where id IN: sAccId];
    for(Account acc: lstAccount){
        for(Contact con: acc.contacts){
            con.Address_1__c = acc.Address_1__c;
            con.Address_2__c = acc.Address_2__c;
            con.City__c=acc.City__c;
            con.Country__c=acc.Country__c;
            lstConUpdate.add(con);
        }
    }
    if(lstConUpdate.size() > 0){
        update lstConUpdate;
    }   
}
Ajay K DubediAjay K Dubedi


Hi Kiran,
Below is the complete Solution for the same:
//Apex Trigger  

trigger ContactTrigger on Contact (after insert , before update) {
    if(Trigger.isafter && Trigger.isinsert){
        ClassTestAccount.Test_ClassTestAccount(Trigger.new);
    }
	else if(Trigger.isbefore && Trigger.isupdate){
        ClassTestAccount.Test_ClassTestAccountbefore(Trigger.new);
    }
}

//Apex Class Code

public class ClassTestAccount {
    public static void Test_ClassTestAccount(List <Contact> ctlist){
        List<contact> lstConUpdate = new List<Contact>();
        set<Id> accid = new set<Id>();
        for(Contact con: ctlist){
            if(con.AccountId != null){
                accid.add(con.AccountId);
            }
        }
    List<Account> Accountlist = [select id, Address_1__c , Address_2__c ,City__c,Country__c ,(select id,Address_1__c ,Address_2__c ,City__c,Country__c from Contacts) from Account where id IN: accid];
        if(Accountlist.size()>0){
            for(Account acc: Accountlist){  
                for(Contact cnt: acc.Contacts){
                     Contact ct = new Contact();
                     ct.id=cnt.Id;
                     ct.Address_1__c = acc.Address_1__c;
                     ct.Address_2__c = acc.Address_2__c;
                     ct.City__c = acc.City__c;
                     ct.Country__c = acc.Country__c;  
                     lstConUpdate.add(ct);  
                }  
            }
            update lstConUpdate;
        }
      }
  public static void Test_ClassTestAccountbefore(List <Contact> ctlist){
        List<contact> lstConUpdate = new List<Contact>();
        set<Id> accid = new set<Id>();
        for(Contact con: ctlist){
            if(con.AccountId != null){
                accid.add(con.AccountId);
            }
        }
    List<Account> Accountlist = [select id, Address_1__c , Address_2__c ,City__c,Country__c from Account where id IN: accid];
        if(Accountlist.size()>0){
            for(Account acc: Accountlist){
                for(Contact ct: ctlist){
                     ct.Address_1__c = acc.Address_1__c;
                     ct.Address_2__c = acc.Address_2__c;
                     ct.City__c = acc.City__c;
                     ct.Country__c = acc.Country__c;
                }
            }
        }
    }
}
Regards,
Ajay