Email Notification Apex Trigger #inSalesforce
trigger sendEmailNotification on Contact (after insert,after update){
for(Contact theContact:trigger.New){
String[] toAddresses = new String[] {theContact.Email};
String messageBody;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
if(trigger.isInsert){
mail.setSubject('Welcome eMail');
messageBody = '<html><body>Hello ' + theContact.Email + ',<br>Hello World. Welcome to Salesforce.<br><br><b>Regards,</b><br>Thank You</body></html>';
mail.setHtmlBody(messageBody);
}
if(trigger.isUpdate){
mail.setSubject('Your Updated Info');
messageBody = '<html><body>Hello ' + theContact.Email + ',<br>Updates completed.<br><br>Contact us for any further queries.</body></html>';
mail.setHtmlBody(messageBody);
}
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
for(Contact theContact:trigger.New){
String[] toAddresses = new String[] {theContact.Email};
String messageBody;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
if(trigger.isInsert){
mail.setSubject('Welcome eMail');
messageBody = '<html><body>Hello ' + theContact.Email + ',<br>Hello World. Welcome to Salesforce.<br><br><b>Regards,</b><br>Thank You</body></html>';
mail.setHtmlBody(messageBody);
}
if(trigger.isUpdate){
mail.setSubject('Your Updated Info');
messageBody = '<html><body>Hello ' + theContact.Email + ',<br>Updates completed.<br><br>Contact us for any further queries.</body></html>';
mail.setHtmlBody(messageBody);
}
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Visual: https://www.youtube.com/watch?v=vdBpxhEqaME
Comments
Post a Comment