Trigger: When Account Email get updated all related Contact Email Should get updated

Example: When Account Email get updated all related Contact Email Should get updated.

Solution

Solution 1: Handle single record.

trigger AccountTrigger on Account (before insert) {
    if(Trigger.isUpdate && Trigger.isAfter) { 
        List<Contact> allAssoContact = [Select Id, Email From Contact Where AccountId =: trigger.new limit 1];
     List<Contact> updateContact = new List<Contact>();
     for(Id accountId : Trigger.newMap.keySet()) { 
         if(trigger.oldMap.get(accountId).Email__c != Trigger.newMap.get(accountId).Email__c)  {
             Contact con = new Contact(Id=allAssoContact[0].Id);
             con.Email = trigger.oldMap.get(accountId).Email__c;
             updateContact.add(con);
         }
     }
     if(!updateContact.isEmpty()) { 
         update updateContact;
     }
 }
}

Leave a Reply