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
Prakhyat sapraPrakhyat sapra 

if account billing address is update then related contact mailingaddress is also update???

if account  billing address is update then related contact mailingaddress is also update???
Best Answer chosen by Prakhyat sapra
Prakhyat sapraPrakhyat sapra
THE CORRECT ANSWER OF THE QUESTION IS CLASS PART public class AccountTriggerHandler1 { // public static void beforeInsert(List newList, Map oldAccountMap){ // UpdateUpdatecontact( newList,oldAccountMap); // } public static void beforeUpdate(List newList,Map oldAccountMap){ UpdateUpdatecontact( newList,oldAccountMap); } public static void UpdateUpdateContact(List newList, Map oldAccountMap){ Map accountIdMap = new Map(); List updatedContacts = new List(); for(Account acc : newList){ if(acc.BillingStreet != oldAccountMap.get(acc.id).BillingStreet || acc.BillingCity != oldAccountMap.get(acc.id).BillingCity || acc.BillingState != oldAccountMap.get(acc.id).BillingState || acc.BillingPostalCode !=oldAccountMap.get(acc.id).BillingPostalCode || acc.BillingCountry != oldAccountMap.get(acc.id).BillingCountry) { accountIdMap.put(acc.Id, acc); System.debug('First debug ::' + accountIdMap); } } List con = [SELECT Id,AccountId,MailingStreet,MailingCity,MailingPostalCode,MailingCountry,MailingState FROM Contact WHERE AccountId IN :Trigger.new ]; System.debug('Second debug ::' + con); Account acco; for(Contact cont : con){ acco = accountIdMap.get(cont.AccountId); cont.MailingStreet = acco.BillingStreet; cont.MailingCity = acco.BillingCity; cont.MailingState = acco.BillingState; cont.MailingPostalCode = acco.BillingPostalCode; cont.MailingCountry = acco.BillingCountry; updatedContacts.add(cont); System.debug('Third Debug ::' + updatedContacts); } if(!updatedContacts.isEmpty()){ update updatedContacts; } } } TRIGGER PART trigger AccountTrigger1 on Account (Before Update) { if(Trigger.isBefore){ if(Trigger.isUpdate){ AccountTriggerHandler1.beforeUpdate(Trigger.New,Trigger.oldMap); } } }

All Answers

SwethaSwetha (Salesforce Developers) 
HI Prakhyat ,
Try below trigger code
trigger ContactMailAddr on Contact (before insert, before update) 
{
    set<id> setofaccountIds=new set<id>();
    
    for(Contact contact:trigger.new)
    {
        if(contact.AccountId!=null)
        {
            setofaccountIds.add(contact.AccountId);
        }
    }
    map<id,account> accountmap=new map<id,account>([select id,
                                                    billingCity,
                                                    billingState,
                                                    billingpostalcode,
                                                    billingStreet,
                                                    billingCountry from account where id in :setofaccountIds]);
    for(contact contact :trigger.new)
    {
            contact.MailingCity = accountmap.get(contact.AccountId).billingCity;
            contact.MailingState = accountmap.get(contact.AccountId).billingState;
            contact.MailingStreet = accountmap.get(contact.AccountId).billingStreet;
            contact.MailingCountry = accountmap.get(contact.AccountId).billingCountry;
            contact.MailingPostalCode = accountmap.get(contact.AccountId).billingpostalcode;
    }
}
Related: https://developer.salesforce.com/forums/?id=906F0000000AxLvIAK
https://developer.salesforce.com/forums/?id=906F0000000kGOuIAM

If this information helps, please mark the answer as best. Thank you
SwethaSwetha (Salesforce Developers) 
Also recommend reviewing https://stackoverflow.com/questions/43013248/create-a-process-to-update-child-record-when-the-parent-is-updated which shows Process Builder approach.

 
Vineela ProddaturuVineela Proddaturu
Hi,Prakhyat
Try like this.
trigger AccountTrigger02 on Account (after insert,after update) {
    if(Trigger.isAfter && Trigger.isInsert){
        list<Account> lisAcc = new list<Account>();
        list<Contact> listContacts = new list<Contact>();
        map<Id,Account> mapOldAcc = new map<Id,Account>();
        set<id> accids = new set<id>();
        for(Account acc : trigger.new){
            System.debug('===Old phone Number==='+mapOldAcc.get(acc.id).BillingAddress);
            Account oldacc = mapOldAcc.get(acc.id);
            if(oldacc.BillingAddress != acc.BillingAddress){
                accids.add(acc.id);
            }
        }
                
        list<contact> listCons = [SELECT Id,Name,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,
                                 AccountId,Account.BillingStreet,Account.BillingCity,Account.BillingState,Account.BillingPostalCode,
                                  Account.BillingCountry FROM Contact WHERE accountid in : accids];        
                                  
       
        for(Contact con : listCons){
            con.MailingStreet = con.account.BillingStreet;
            con.MailingCity = con.account.BillingCity;
            con.MailingState = con.account.BillingState;
            con.MailingPostalCode = con.account.BillingPostalCode;
            con.MailingCountry = con.account.BillingCountry;
            listContacts.add(con);
            
        }try{
            if(listContacts.size() > 0 || listContacts.isEmpty()){
                update listContacts;
            }
        }Catch(DMLException e){
            System.debug('Following error message occured'+e);
        }
    }
    }
Prakhyat sapraPrakhyat sapra
THE CORRECT ANSWER OF THE QUESTION IS CLASS PART public class AccountTriggerHandler1 { // public static void beforeInsert(List newList, Map oldAccountMap){ // UpdateUpdatecontact( newList,oldAccountMap); // } public static void beforeUpdate(List newList,Map oldAccountMap){ UpdateUpdatecontact( newList,oldAccountMap); } public static void UpdateUpdateContact(List newList, Map oldAccountMap){ Map accountIdMap = new Map(); List updatedContacts = new List(); for(Account acc : newList){ if(acc.BillingStreet != oldAccountMap.get(acc.id).BillingStreet || acc.BillingCity != oldAccountMap.get(acc.id).BillingCity || acc.BillingState != oldAccountMap.get(acc.id).BillingState || acc.BillingPostalCode !=oldAccountMap.get(acc.id).BillingPostalCode || acc.BillingCountry != oldAccountMap.get(acc.id).BillingCountry) { accountIdMap.put(acc.Id, acc); System.debug('First debug ::' + accountIdMap); } } List con = [SELECT Id,AccountId,MailingStreet,MailingCity,MailingPostalCode,MailingCountry,MailingState FROM Contact WHERE AccountId IN :Trigger.new ]; System.debug('Second debug ::' + con); Account acco; for(Contact cont : con){ acco = accountIdMap.get(cont.AccountId); cont.MailingStreet = acco.BillingStreet; cont.MailingCity = acco.BillingCity; cont.MailingState = acco.BillingState; cont.MailingPostalCode = acco.BillingPostalCode; cont.MailingCountry = acco.BillingCountry; updatedContacts.add(cont); System.debug('Third Debug ::' + updatedContacts); } if(!updatedContacts.isEmpty()){ update updatedContacts; } } } TRIGGER PART trigger AccountTrigger1 on Account (Before Update) { if(Trigger.isBefore){ if(Trigger.isUpdate){ AccountTriggerHandler1.beforeUpdate(Trigger.New,Trigger.oldMap); } } }
This was selected as the best answer