Email Send to Case contact
When case becomes escalated then email send to associated contact.
Apex Trigger:-
trigger caseTrger on Case(After update){
contactHandler handlerClass = new contactHandler();
Set<Id> contIds = new Set<Id>();
for(Case cs : Trigger.New){
if(cs.Status == 'escalated')
contIds.add(cs.contactId);
}
if(contIds.size() > 0){
handlerClass.sendEmails(contIds);
}
}
Apex class:-
public class contactHandler{
public void sendEmails(List<String> ConList){
// Step 0: Create a master list to hold the emails we'll send
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
For(Contact con : [Select Id, FirstName, Email from Contact where Id IN: ConList]){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Step 1: Set list of people who should get the email
List<String> sendTo = new List<String>();
sendTo.add(con.Email);
mail.setToAddresses(sendTo);
mail.setSenderDisplayName('Official Bank of Notification');
// Step 3. Set email contents - you can use variables!
mail.setSubject('Case has been Escalated
String body = 'Dear ' + con.FirstName + ', ';
body += 'Your case has been escalated';
mail.setHtmlBody(body);
mails.add(mail);
}
// Step 4: Send all emails in the master list
if(mails.size() > 0) {
try{
Messaging.sendEmail(mails);
}catch(exception e){
System.debug('getMessage-->>>>>> '+e.getMessage());
// apexpages.addmessage(new apexpages.message(apexpages.severity.error,e.getMessage()));
}
}
}
}