Trigger – Update All Account Related Contacts

Trigger to update account related contacts
Trigger to update account related contacts

Here is apex trigger to update account related contacts when Email of account is updated. We have created a new custom fields on account named as email and if email got updated we want to set that email to all the associated contacts.

				
					trigger AccountTrigger2 on Account(after update) {

    Set < Id > accountIdSet = new Set < Id > ();

    for (Id accountId: Trigger.NewMap.keySet()) {
        if (Trigger.OldMap.get(accountId).Email__c != Trigger.NewMap.get(accountId).Email__c) {
            accountIdSet.add(accountId);
        }
    }

    List < Contact > lstContact = [SELECT id, Email, AccountId FROM Contact where      AccountId In: accountIdSet];

    List < Contact > contactToUpdate = new List < Contact > ();
    for (Contact con: lstContact) {
        Contact contact = new Contact(Id = con.Id);
        contact.Email = Trigger.NewMap.get(con.AccountId).Email__c;
        contactToUpdate.add(contact);

    }
    if (!contactToUpdate.isEmpty()) {
        update contactToUpdate;
    }
}
				
			

In this video we have discussed the trigger in detail.

How the trigger works

  1. The trigger runs after update on Account, because the new values are already saved when it runs.
  2. It loops through Trigger.NewMap and compares the old and new Email__c of each Account. Only Accounts whose email really changed are collected.
  3. One query fetches all Contacts that belong to those Accounts.
  4. For each Contact it creates a lightweight record containing only the Id and the new Email, then updates the whole list once. Updating only the changed field avoids overwriting other fields.

Small improvements

  • If no Email__c changed, the Set is empty, but the query still runs and uses one of your 100 queries. Add a check for an empty Set first.
  • If the Account email is cleared, the Contacts email will be cleared too. That is usually what you want, but confirm it with your business team.
  • Updating Contacts runs any Contact triggers and flows too, and an invalid email format will stop the save.
trigger AccountTrigger2 on Account (after update) {
    Set<Id> accountIdSet = new Set<Id>();
    for (Account acc : Trigger.new) {
        if (acc.Email__c != Trigger.oldMap.get(acc.Id).Email__c) {
            accountIdSet.add(acc.Id);
        }
    }
    if (accountIdSet.isEmpty()) {
        return;
    }
    List<Contact> contactToUpdate = new List<Contact>();
    for (Contact con : [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accountIdSet]) {
        contactToUpdate.add(new Contact(Id = con.Id, Email = Trigger.newMap.get(con.AccountId).Email__c));
    }
    update contactToUpdate;
}

How to test

  1. Create an Account with Email__c filled in and add three Contacts to it.
  2. Change Email__c on the Account and save. All three Contacts should show the new email.
  3. Save the Account again without changing the email. The Contacts should not change.

FAQ

Why create a new Contact instead of changing the queried one?
Setting only the Id and the changed field keeps the update small and avoids touching other fields.

Why after update and not before update?
We change Contacts, not the Account, so after update is correct and the Account is already saved.

Leave a Reply