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
Ganesh Patil 78Ganesh Patil 78 

Send an email using triggers to contacts related to the accounts when account is updated. If 2 contacts has same email than send only 1 mail to that contacts having similar emails. In the email body mention the name of contacts having similar emails.

Send an email using triggers to contacts related to the accounts when account is updated. If 2 contacts has same email than send only 1 mail to that contacts having similar emails. In the email body mention the name of contacts having similar emails in a tabular format. For example if the Account A1 has 5 contacts c1, c2, c3, c4, c5 and c1, c2, c3 has similar emails and other having unique email than the names of the contact should be show in the table in the email body of the contacts having similar emails.
Best Answer chosen by Ganesh Patil 78
Maharajan CMaharajan C
Hi Ganesh,

The below trigger will fire whenever account name is updated and it will send email to relatad contacts as per your logic...

Please modify if there is any changes required.

Please try the below trigger:
 
trigger sendEmailtoContact on Account (after update) {
    
    set<Id> accIds = new set<Id>();
    Map<String,List<string>> emailNameMap = new Map<String,List<string>>();
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    set<string> emailalreadysentSet = new set<string>();
    
    for(Account acc : trigger.new){
        if(acc.Name != Trigger.oldMap.get(acc.Id).Name){
            accIds.add(acc.Id);
        }
    }
    List<contact> contacts = [Select Id,Email,AccountId,Name from Contact where AccountId IN: accIds];
    for(contact con : contacts){
        if(con.Email != null){
            string key = con.Email + '-' + con.AccountId;
            if(!emailNameMap.containsKey(key)){
                emailNameMap.put(key, new List<String>{con.Name});
            }
            else{
                emailNameMap.get(key).add(con.Name);
            }
        }
    }    
    
    for(contact c : contacts){
        if(c.Email != null){
            String key = c.Email + '-' + c.AccountId;
            if(!emailalreadysentSet.contains(key)){
                system.debug(' -- 3 -- ');
                emailalreadysentSet.add(key);
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                List<String> sendTo = new List<String>();
                sendTo.add(c.Email);
                mail.setToAddresses(sendTo);
                mail.setSenderDisplayName('Salesforce Dev Team');
                mail.setSubject('Salesforce Contact Email');
                String body = 'This Email is sent from your Salesforce Account <br/> <br/>';
                List<String> emailList = emailNameMap.get(key);
                if(emailList.size() > 1){
                    body += '<html><h2 style=\"text-align:left;\">Duplicate Contacts Found</h2><br/>';
                    body += '<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFFF;"  cellpadding="3" cellspacing="3">';
                    body += '<tr class="headerRow" style="background-color: #99CCFF; font-weight: bold;"><td align="center">Name</td><td align="center">Email</td></tr>';
                    for(string str : emailList){
                        body += '<tr><td align="center" style="font-size:11px">' + str +'</td><td align="center" style="font-size:11px">'+ c.Email +'</td></tr>';
                    }
                    body += '</table></html>';  
                }
                mail.setHtmlBody(body);
                //mail.setTargetObjectId(c.Id);
                mail.setSaveAsActivity( false );
                mails.add(mail);
            }
        }
    }
    
    system.debug(' mails --> ' + mails.size());
    
    if(!mails.IsEmpty())
        Messaging.sendEmail(mails);    
}

Thanks,
Maharajan.C