Update Child record when parent record is updated

Update Child record when parent record is updated:


Example:

trigger ContactUpdate on Account (after update) {

    Map<Id,  Account> mapAccount = new Map <Id, Account>();

    List<Contact> listContact = new List<Contact>();

    for (Account acc : trigger.new) {

        if (acc.Phone != trigger.oldMap.get(acc.Id).Phone)

            mapAccount.put(acc.Id, acc);

    }

    if (mapAccount.size() > 0) {

        listContact = [ SELECT id, Phone FROM Contact WHERE AccountId IN : mapAccount.keySet() ];

        if(listContact.size() > 0)  {

            for(Contact con : listContact)  {

                con.Phone = mapAccount.get(con.AccountId).Phone;

            }

            update listContact;

        }

    }

}


Comments