WAT on Account when phone is update then send the mail

trigger sendMailOnAccoutPhoneUpdate on Account (after update) {

Set<Id> setAccountId= new Set<Id>();
List<Contact> lstContact= new List<Contact>();
List<String> lstEmail= new List<String>();

for(Account acc:trigger.new)
{
if(acc.phone != trigger.oldmap.get(acc.id).phone)
{
   setAccountId.add(acc.id);
}
}
if(setAccountId!= null && setAccountId.size()>0)
{

  lstContact = [SELECT Id, Email, Name FROM Contact WHERE AccountId IN: setAccountId];

 if(lstContact != null && lstContact.size()>0)
 {
   for(Contact objCon:lstContact)
   {
    if(objCon.Email != NULL)
                {
                                   
                    lstEmail.add(objCon.Email);
                }
   }
 
 }
}
if(lstEmail != NULL && lstEmail.size()>0)
    {
        Messaging.SingleEmailMessage objMail = new Messaging.SingleEmailMessage();
        objMail.setToAddresses(lstEmail);
        objMail.setPlainTextBody('Your account information has been updated successfully.');
        objMail.setsubject('Account Update Info');
        Messaging.sendEmail(new Messaging.SingleEmailMessage [] {objMail});
    }


}

Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page