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
Kiran ChodavadiyaKiran Chodavadiya 

How to trigger the email notification to the Account owner for any update in email field of contact by Apex coding?

Hi,
I'd like to set up the email notification for email update in contact field. once existing email field has been update, Account owner of this related contact recieve email notification.
Best Answer chosen by Kiran Chodavadiya
varun kumar 212varun kumar 212
Hi Kiran,

Please use the below code.

 
trigger ContactTrigger on Contact (After update) {
    if(trigger.isAfter && trigger.isUpdate){
        Set<Id> accIds = new Set<Id>();
        Map<Id,String> accOwnerMap = new Map<Id,String>();
        List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
        for(Contact c : trigger.new){
            accIds.add(c.AccountId);
        }
        if(accIds.size()>0){
            List<Account> accList = [select Id,owner.Email from Account where Id In: accIds];
            if(accList.size()>0){
                for(Account acc : accList){
                    accOwnerMap.put(acc.Id,acc.owner.Email);
                }
            }
        }
        for(Contact c : trigger.new){
            if(c.Email != null && c.Email != trigger.oldmap.get(c.Id).Email){
                if(accOwnerMap.get(c.AccountId) != null){
                    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                    System.debug(c.Account);
                    message.toAddresses = new String[] { accOwnerMap.get(c.AccountId) };
                        message.subject = 'Contact Email Changed';
                    message.plainTextBody = 'Contact Email Changed';
                    lstMsgs.add(message);
                }
            }
        }
        if(lstMsgs.size()>0){
            Messaging.SendEmailResult[] results = Messaging.sendEmail(lstMsgs);
            if (results[0].success) {
                System.debug('The email was sent successfully.');
            } else {
                System.debug('The email failed to send: '
                             + results[0].errors[0].message);
            }
        }
    }
}

 

All Answers

varun kumar 212varun kumar 212
Hi Kiran,

Please use the below code.

 
trigger ContactTrigger on Contact (After update) {
    if(trigger.isAfter && trigger.isUpdate){
        Set<Id> accIds = new Set<Id>();
        Map<Id,String> accOwnerMap = new Map<Id,String>();
        List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
        for(Contact c : trigger.new){
            accIds.add(c.AccountId);
        }
        if(accIds.size()>0){
            List<Account> accList = [select Id,owner.Email from Account where Id In: accIds];
            if(accList.size()>0){
                for(Account acc : accList){
                    accOwnerMap.put(acc.Id,acc.owner.Email);
                }
            }
        }
        for(Contact c : trigger.new){
            if(c.Email != null && c.Email != trigger.oldmap.get(c.Id).Email){
                if(accOwnerMap.get(c.AccountId) != null){
                    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                    System.debug(c.Account);
                    message.toAddresses = new String[] { accOwnerMap.get(c.AccountId) };
                        message.subject = 'Contact Email Changed';
                    message.plainTextBody = 'Contact Email Changed';
                    lstMsgs.add(message);
                }
            }
        }
        if(lstMsgs.size()>0){
            Messaging.SendEmailResult[] results = Messaging.sendEmail(lstMsgs);
            if (results[0].success) {
                System.debug('The email was sent successfully.');
            } else {
                System.debug('The email failed to send: '
                             + results[0].errors[0].message);
            }
        }
    }
}

 
This was selected as the best answer
Kiran ChodavadiyaKiran Chodavadiya
Hi, Varun 
Thanks for helping me out for this.
could you please help me to find where i did mistake in following code?. its not working properly. 


trigger SendEmailtoAccountOwner on Contact (After Update) {
    List<messaging.SingleEmailMessage> emails = new List<messaging.SingleEmailMessage>();
    
    // getting updated Contact's recordid
    List<id> ContactId = new List<id>();
    for (Contact con: Trigger.new) {
        contact oldcon = trigger.oldMap.get(con.id);
        if(con.email != oldcon.email) {
             id VarId = con.Id; 
             ContactId.add(VarId);
        }
      }
    //related Accounts which related contact has been updated.
           List<Account> RelatedAccountList = new List<Account>();
           RelatedAccountList = [SELECT id,OwnerId From Account Where id In:ContactId];
     
        List<Id> ownerIds = new List<id>();
        
    for (Account a :RelatedAccountList) {
        if (RelatedAccountList.size()>0) {
            id ownerId = a.OwnerId;
            ownerIds.add(ownerId);
       } 
    }
    if(ownerIds.size()>0) {
          List<User> users = new List<User>();
          users = [SELECT id,Name,Email From User Where id in:OwnerIds];
        if (Users.size()>0){
            List<String> sendTo = new List<String>();
            List<String> userName = new List<String>();
            for(user u:users){
              sendTo.add(u.email);
              userName.add(u.Name);
            }
            messaging.SingleEmailMessage email= new messaging.SingleEmailMessage();
            email.setToAddresses(sendTo);
            email.setSubject('Email field Updation');
            email.setPlainTextBody('Hello'+userName+'Your Related Contact email field has Been Updated');
            emails.add(email);
            
            
            messaging.sendEmail(emails);
        }
        
    }
    

}
Kiran ChodavadiyaKiran Chodavadiya
No Worry!! i found a solution where i was doing mistake to getting contact"s id instead of AccountId At line No 9