+ Start a Discussion
Todd B.Todd B. 

Trying to Send an Email based on an Event

Since you can not use workflows to send an email update on a Event, I am working on creating a trigger that will send email.  I created an email template called MCC_Close_Event_Email and based it on the Event object.  I tested it and made sure it pulls all the correct info when sending.  

Then I created the trigger below to send the email when a cretain field is checked.  
trigger EventSendEmailMCCReminder on Event (After Update, After Insert) {

/*  Event e1 = trigger.new[0];  */
    For (Event e1 : trigger.New){
  String[] toAddresses = new String[] {e1.owner.email};
  String[] strProfile = New String[] {e1.owner.profile.name};    
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

  mail.setTargetObjectId(e1.OwnerID);
  mail.setSenderDisplayName('Salesforce Admin');
  mail.setUseSignature(false);
  mail.setBccSender(false);
  mail.setSaveAsActivity(false);  
  
  System.debug('Profile Name ' + e1.owner.profile.name);
  System.debug('Owner Name ' + e1.owner.name);  
  System.debug('Profile Name ' + strProfile);
  System.debug('To Address ' + toAddresses);        
    
	If(e1.SendMCCRemiderEmail__c == True ){
 /*       If(e1.owner.profile.name == 'System Administrator'){ */
		EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'MCC_Close_Event_Email'];
        mail.setTemplateId(et.id);
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});         
    }   
    }  
}
My issue is, it send the email, but all the Event related fields in the email are blank.  Also, all me System.Debug values are blank in the debug log.  

Any ideas to ensure the Event related fields in the Email are populated?
 
Best Answer chosen by Todd B.
Todd B.Todd B.
Hello Anupam, 

After reviewing what you said, I added one line of code to my trigger:
mail.setWhatId(e1.id);
and now everything works fine, including using an email template.  

I ported eveything over to a class and the final code looks like this:
public class MCCSendEmail {
        
    public MCCSendEmail(){}
    public void SendEmail(Event[] newEvents){
    
 For (Event e1 : newEvents){
  String[] toAddresses = new String[] {e1.owner.email};
 
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

  mail.setTargetObjectId(e1.OwnerID);
  mail.setWhatId(e1.id);     
  mail.setSenderDisplayName('Salesforce Admin');
  mail.setUseSignature(false);
  mail.setBccSender(false);
  mail.setSaveAsActivity(false);  
      
  EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'MCC_Close_Event_Email'];
  mail.setTemplateId(et.id);
        
  Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});         
        }   
    }  
                   
    }

Now, I have a time based workflow, check a box on the Event record, then a trigger, on the event record, calls the class once the aforementioned box is check.  

A nice, simple, salesforce.com solution...NOT!  But at least it works.  

All Answers

Anupam RastogiAnupam Rastogi
Hi Todd,

I also tried out a similar situation wherein I tested an email trigger on an Event update.

The values from the event fields are not getting populated in the mail because the WhatId is not mentioned for the email being sent. Therefore the system does not know which Event record to use for merging fields.
Now, when you provide the WhatId using setWhatId(param1) it errors out stating that cannot use WhatId when using UserIds.

So the other way to achieve the desired results is to NOT use the email templates.
Instead build the body and subject of the message within the trigger using setToAddresses(List<String>), setSubject(String) and setHtmlBody(String).

I tried out using this option and was able to pull information from the event and place it in the email successfully.

Let me know if you need any further assistance.

Thanks
AR

If your problem resolves by this reply then please mark it as best answer.
Todd B.Todd B.
Hello Anupam, 

After reviewing what you said, I added one line of code to my trigger:
mail.setWhatId(e1.id);
and now everything works fine, including using an email template.  

I ported eveything over to a class and the final code looks like this:
public class MCCSendEmail {
        
    public MCCSendEmail(){}
    public void SendEmail(Event[] newEvents){
    
 For (Event e1 : newEvents){
  String[] toAddresses = new String[] {e1.owner.email};
 
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

  mail.setTargetObjectId(e1.OwnerID);
  mail.setWhatId(e1.id);     
  mail.setSenderDisplayName('Salesforce Admin');
  mail.setUseSignature(false);
  mail.setBccSender(false);
  mail.setSaveAsActivity(false);  
      
  EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'MCC_Close_Event_Email'];
  mail.setTemplateId(et.id);
        
  Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});         
        }   
    }  
                   
    }

Now, I have a time based workflow, check a box on the Event record, then a trigger, on the event record, calls the class once the aforementioned box is check.  

A nice, simple, salesforce.com solution...NOT!  But at least it works.  
This was selected as the best answer
Fermin Campo 1Fermin Campo 1
Hi, i have the same problem, but I need to use email templates because I need to send pics and html version in a email. What Can I do?