Skip to content

Instantly share code, notes, and snippets.

@aakashjain
Last active August 29, 2015 14:25
Show Gist options
  • Save aakashjain/01e82fef1c316dda0bee to your computer and use it in GitHub Desktop.
Save aakashjain/01e82fef1c316dda0bee to your computer and use it in GitHub Desktop.
Notification email trigger for VisitorCenter app
trigger VisitorNotify on Visitor__c (before update) {
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Map<Id, Visitor__c> emps = new Map<Id, Visitor__c>([select Id, User__r.Name, User__r.Email
from Visitor__c where Id in :Trigger.new]);
for (Visitor__c v : Trigger.new) {
Visitor__c oldv = Trigger.oldMap.get(v.Id);
if (v.Status__c == 'Checkedin' && oldv.Status__c == 'Pending') {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Visitor__c emp = emps.get(v.id);
List<String> sendTo = new List<String>();
sendTo.add(emp.User__r.Email);
mail.setToAddresses(sendTo);
mail.setSenderDisplayName('VisitorCenter System');
mail.setSubject('Visitor at ' + String.valueOf(v.Date__c));
String body = 'Hi ' + emp.User__r.Name + ',<br/>This is to notify you that <b>' +
v.FirstName__c + ' ' + v.MiddleName__c + ' ' + v.LastName__c + '</b> has been ' +
'approved to visit you at <b>' + String.valueOf(v.Date__c) + '</b>.<br/>Check the ' +
'VisitorCenter app on your phone for more details.<br/>Please do not reply to ' +
'this system-generated mail.';
mail.setHTMLBody(body);
mails.add(mail);
}
}
Messaging.sendEmail(mails);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment