function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SidMSidM 

Sending a VF email summary of an Event record from Apex

Hi everyone,

 

We have a requirement to send a templated email to the manager of the currently logged in user, summarising several standard and custom fields that reside on the Event object. The email template has been defined in VisualForce and works when doing a merge test from the VisualForce email edit UI and I have written the Apex code below to do send the email itself.

 

Although the email is being sent at the right time and to the right person (which we're triggering via an AJAX call to the Apex class via a Custom Button), it is not correctly related to the Event record (its "relatedTo" object), so doesn't display any Event information.

 

Here's the Apex code that send the email:

 

global class EmailUtilities {

	WebService static boolean sendEventEmailToManager(Id eventId) {
		List<Messaging.SendEmailResult> results;
		
		// Ensure we are not going to exceed single send limits by sending this (throws System.LimitException)
		Messaging.reserveSingleEmailCapacity(1);
		
		// Retrieve the user record for this user, including their manager's user record Id
		User user = [Select Id, Name, ManagerId, Email From User Where Id = :UserInfo.getUserId()];
		
		if (user.ManagerId == null) {
			throw new ManagerNotSetException('User ' + user.Name + ' has no manager assigned in their user record.');
		}
		
		// Retrieve the template to be used
		EmailTemplate template = [SELECT id FROM EmailTemplate WHERE developerName = 'EventSummary'];
		
		// Create the message
		Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
		email.setSenderDisplayName(UserInfo.getName());
		email.setReplyTo(user.Email);
		email.setWhatId(eventId);
		email.setSaveAsActivity(false);
		email.setTargetObjectId(user.ManagerId);
		email.setTemplateId(template.Id);
	
		// Send a blind copy to the sender
		email.setBccSender(true);
		
		// Send email
		results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
		
		return results.get(0).isSuccess();
	}
	
	class ManagerNotSetException extends Exception {}
}

 

From looking at the documentation, the WhatId cannot use an Event object, but if we can't use this, then I'm at a loss as to how an Event record to this email template within Apex! Perhaps there is no way to do it, but it seems particularly limiting if so.

 

Does anyone have any pointers? Thanks in advance for any help anyone is able to provide.

rcravenrcraven

The documentation doesn't list Activity related Event and Tasks as being valid WhatIds.   However, out of curiousity, I've mocked up a similar scenario using a Visualforce email template and the email was sent successfully and contained the Event related merge fields.

 

I suggest using a Visualforce email template, and ensure that the recipientType and relatedToType are setup correctly:

 

 

<messaging:emailTemplate subject="Can event be used as whatid" recipientType="User" relatedToType="Event">
</messaging:emailTemplate>

 

 

In addition, I would suggest running the code from the System Log, which if it runs successfully there, it's a permissions issue.  {Check Object CRUD, or Field Level Security}

 

Hope this helps,

-Rob

SidMSidM

Hi Rob,

 

Thanks very much for this and thank you for taking the time to try it out yourself. :-)

 

Interestingly, it seems to be the AJAX Toolkit request via the SOAP API which is the cause of the complication. If I run this command from the System Log, the email comes through fine, with the correct data. When I run the same command via the AJAX toolkit, which obviously makes the call via the SOAP API, it doesn't retrieve the event data.

 

I ran an AJAX request via the SOAP debug tool and I was able to retrieve the record and view all the data and describe the entire object without any issues, but when I make the AJAX call to the static WebService method to send the email, the email comes through without the data again. Very odd indeed!

 

For the moment, I think my fallback will have to be to use a VisualForce page with the "action" parameter set to call the relevant method, but I'll try and do some more digging to see why this specifically doesn't show when called via the AJAX toolkit.

 

Thanks again for your help on this. Much appreciated.

ClaiborneClaiborne

This may help, or not.

 

I have had problems with VisualForce email templates that used <apex:outputfield> tags in a VF component. It works fine in the template tester, but the email never gets sent, and no error is generated.

 

I changed the template to use <apex:outputtext> tags, and it worked.

 

There is nothing in the documentation about this, but if it works, I do it.

SidMSidM

Hi Claiborne,

 

Thanks for your input. It's a little different in this case, as the email is being sent just fine, it just doesn't contain the correct data when it's sent as a SOAP call via the AJAX toolkit, as it doesn't appear to correctly associate the Event record.

 

I haven't modified it to use straight Apex/VF yet, as I proposed in my previous post. I'll be sure to update my findings once I have.

 

Thanks for your help.

 

Cheers