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.

Best Answer chosen by Ganesh Patil 78
CharuDuttCharuDutt
Hii Ganesh Patil 
Try Below Trigger
trigger SendEmailtoContact on Account (after update) {
    set<String> lstConEmail=new set<String>();
   
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Set<Id> setAccountIds = new Set<Id>();
        List<String> sendTo = new List<String>();
         If(Trigger.IsAfter && Trigger.IsUpdate){
        For(Account Acc : trigger.new){
            If(Acc.Active__c == 'Yes' && Trigger.oldMap.get(Acc.Id).Active__c != Acc.Active__c){
              setAccountIds.Add(Acc.Id);
            }
        }
    }

        if(!setAccountIds.isEmpty()) {
            for(Contact c : [SELECT lastname,Email FROM Contact WHERE AccountId IN:setAccountIds]){
                if(string.IsNotBlank(c.Email)){
                    lstConEmail.Add(C.Email);
                }
            }
        }
    sendTo.addAll(lstConEmail);

        if(!sendTo.isEmpty()){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');

            mail.setSubject('Account Name change');
            String body = 'Dear Employee Account Name is change.';
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
            try{
                Messaging.SendEmail(mails);
            }
            catch(Exception e){
                System.debug('-----Exception------' +e);        
            }
        }
}
Please Mark It As Best Answer If It Helps
Thank You!

 

All Answers

CharuDuttCharuDutt
Hii Ganesh Patil 
Try Below Trigger
trigger SendEmailtoContact on Account (after update) {
    set<String> lstConEmail=new set<String>();
   
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Set<Id> setAccountIds = new Set<Id>();
        List<String> sendTo = new List<String>();
         If(Trigger.IsAfter && Trigger.IsUpdate){
        For(Account Acc : trigger.new){
            If(Acc.Active__c == 'Yes' && Trigger.oldMap.get(Acc.Id).Active__c != Acc.Active__c){
              setAccountIds.Add(Acc.Id);
            }
        }
    }

        if(!setAccountIds.isEmpty()) {
            for(Contact c : [SELECT lastname,Email FROM Contact WHERE AccountId IN:setAccountIds]){
                if(string.IsNotBlank(c.Email)){
                    lstConEmail.Add(C.Email);
                }
            }
        }
    sendTo.addAll(lstConEmail);

        if(!sendTo.isEmpty()){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');

            mail.setSubject('Account Name change');
            String body = 'Dear Employee Account Name is change.';
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
            try{
                Messaging.SendEmail(mails);
            }
            catch(Exception e){
                System.debug('-----Exception------' +e);        
            }
        }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
This was selected as the best answer
CharuDuttCharuDutt
Hii Ganesh Patil 
Please Close Your query By Marking It As Best Answer If It Helps So Iy Helps Others In Future
Thank You!