In this module, you create a trigger that sends confirmation emails to speakers when they are assigned to a session. You create another trigger that rejects double bookings of speakers.
In this step, you create a trigger that sends confirmation emails to speakers when they are assigned to a session.
In the Developer Console, click File > New > Apex Trigger
Specify SendConfirmationEmail as the trigger name, Session_Speaker__c as the sObject, and click Submit
Implement the trigger as follows:
trigger SendConfirmationEmail on Session_Speaker__c (after insert) {
for(Session_Speaker__c newItem : trigger.new) {
// Retrieve session name and time + speaker name and email address
Session_Speaker__c sessionSpeaker =
[SELECT Session__r.Name,
Session__r.Session_Date__c,
Speaker__r.First_Name__c,
Speaker__r.Last_Name__c,
Speaker__r.Email__c
FROM Session_Speaker__c WHERE Id=:newItem.Id];
// Send confirmation email if we know the speaker's email address
if (sessionSpeaker.Speaker__r.Email__c != null) {
String address = sessionSpeaker.Speaker__r.Email__c;
String subject = 'Speaker Confirmation';
String message = 'Dear ' + sessionSpeaker.Speaker__r.First_Name__c +
',\nYour session "' + sessionSpeaker.Session__r.Name + '" on ' +
sessionSpeaker.Session__r.Session_Date__c + ' is confirmed.\n\n' +
'Thanks for speaking at the conference!';
EmailManager.sendMail(address, subject, message);
}
}
}
In a real-life application, hardcoding the email message is not a recommended approach. Consider using email templates instead.
Save the file. The trigger takes effect as soon as you save it.
Test the trigger
Sending a confirmation email when a speaker is assigned to a session (in other words, when a Session_Speaker__c record is created) can be accomplished without writing code by defining a workflow rule. The programmatic approach used in this workshop allows you to satisfy additional requirements. For example, sending a confirmation email "on demand", independently of a record being created or updated.
In the Developer Console, click File > New > Apex Trigger
Specify RejectDoubleBooking as the trigger name, Session_Speaker__c as the sObject, and click Submit
Implement the trigger as follows:
trigger RejectDoubleBooking on Session_Speaker__c (before insert, before update) {
for(Session_Speaker__c sessionSpeaker : trigger.new) {
// Retrieve session information including session date and time
Session__c session = [SELECT Id, Session_Date__c FROM Session__c
WHERE Id=:sessionSpeaker.Session__c];
// Retrieve conflicts: other assignments for that speaker at the same time
List<Session_Speaker__c> conflicts =
[SELECT Id FROM Session_Speaker__c
WHERE Speaker__c = :sessionSpeaker.Speaker__c
AND Session__r.Session_Date__c = :session.Session_Date__c];
// If conflicts exist, add an error (reject the database operation)
if(!conflicts.isEmpty()){
sessionSpeaker.addError('The speaker is already booked at that time');
}
}
}
Save the file
Test the trigger:
RejectDoubleBooking is not sufficient to entirely prevent the double booking of speakers. For example, the user could change the date and time of a session after the facts in a way that creates a double booking for a speaker assigned to that session. You could create an additional trigger to take care of that situation.