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
chidambarakumari rchidambarakumari r 

How to trigger for this scenario "if account email is changed change all associated contact email id which initially had same email id as that of the account"

Best Answer chosen by chidambarakumari r
Bishal Singh 12Bishal Singh 12
trigger addContactEmail on Account(after update) {
Set<id> accoun = new Set<id>();

for(Account acc : Trigger.old)
{
accoun.add(acc.id);
}
List<contact> con = [select id,email from Contact where accountid IN :accoun];
for(Account a : Trigger.New)
 {
 if(trigger.oldMap.get(a.id).email__c != trigger.newMap.get(a.id).email__c)
    {
      for(Contact c : con)
      {
        c.email = a.email__c;
         }
      }           
 }
  update con;

}

All Answers

PrasathPrasath
Hi chidambarakumari,

Write a trigger on Account object with after update event, on after update get the contact associate with the account and update the contact records with account email. 
Vaishnavi GiriVaishnavi Giri
Hi chidambarakumari,
Try this and let me know.
Account field - Email__c
Contact Field - Email
public void updateContactEmail(List<Account> accountnew,Map<ID,Account> oldAccount){
        Map<String,Account> accountRec = new Map<String,Account>();
        Map<ID,Account> accountEmailRec = new Map<ID,Account>();
        for(Account record : oldAccount.values()){
            accountRec.put(record.ID,oldAccount.values());
        }
        List<Contact> contactEmailRec = new List<Contact>();
        for(Account emailAcc : accountnew){
            accountEmailRec.put(emailAcc.ID,emailAcc);
        }
        
        for(Contact conRec : [SELECT Id,Email,AccountId from contact where AccountId IN :oldAccount.keySet()]){
            for(Id accRec : oldAccount.keyset()){
                if(conRec.AccountId == oldAccount.get(accRec).Id){
                    if(conRec.Email == accountRec.get(conRec.AccountId).Email__c)     {
                       conRec.Email = accountEmailRec.get(conRec.AccountId).Email__c;
                        
                    }
                }
            }
            contactEmailRec.add(conRec);
        }
        update(contactEmailRec);
    }

Bishal Singh 12Bishal Singh 12
trigger addContactEmail on Account(after update) {
Set<id> accoun = new Set<id>();

for(Account acc : Trigger.old)
{
accoun.add(acc.id);
}
List<contact> con = [select id,email from Contact where accountid IN :accoun];
for(Account a : Trigger.New)
 {
 if(trigger.oldMap.get(a.id).email__c != trigger.newMap.get(a.id).email__c)
    {
      for(Contact c : con)
      {
        c.email = a.email__c;
         }
      }           
 }
  update con;

}
This was selected as the best answer