I am trying to create a VisualForce Email Template, related to the Case Object. In the template, I wish to include the email correspondence we had with the customer as a thread. Below are the Apex Class, Controller, and VF Email Template I assembled. Currently, I am receiving the below error trying to test the template:
Apex Class - 'CaseEmailExtension':
public with sharing class CaseEmailExtension {
public CaseEmailExtension() {}
private final Case currentCase;
public CaseEmailExtension(ApexPages.StandardController currentcase) {
this.currentCase = (Case)currentcase.getRecord();
}
public List<EmailMessage> getSortEmails(){
List <EmailMessage> sortedEmails = new List<EmailMessage>();
sortedEmails = [SELECT Id, FromAddress, ToAddress, BCCAddress, MessageDate, Subject, HasAttachment, Incoming, HtmlBody, CreatedBy.Name //, (Select Id, Name from Attachments) --> attachments are not needed at the separate emails
from EmailMessage where ParentId =: currentCase.Id
order by MessageDate DESC ];
return sortedEmails;
}
}
VisualForce Component - 'CaseEmailExtension':
<apex:component controller="CaseEmailExtension" access="global">
<apex:attribute name="caseId" type="Id" description="Case Id" />
<table border = "1">
<apex:repeat value="{!sortEmails}" var="email">
<apex:facet name="header">
<apex:outputText value="{0,date,dd'.'MM'.'yyyy HH:mm:ss z}" style="font-weight:bold;font-style:italic;font-size:15px;float:left">
<apex:param value="{!email.MessageDate}" />
</apex:outputText>
</apex:facet>
<table width="100%">
<tr>
<td width="70%" align="left">
<table cellpadding="2px">
<tr>
<td width="50px" align="right"><b>From: </b></td>
<td align="left"><i><apex:outputText value="{!email.FromAddress}"/></i></td>
</tr>
</table>
</td>
<td width="30%" align="right">
<b>{!IF(email.Incoming,"INCOMING", "OUTGOING")}</b>
</td>
</tr>
<tr>
<td width="70%" align="left">
<table cellpadding="2px">
<tr>
<td width="50px" align="right"><b>To: </b></td>
<td align="left"><i><apex:outputText value="{!email.ToAddress}"/></i></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="70%" align="left">
<table cellpadding="2px">
<tr>
<td width="50px" align="right"><b>BCC: </b></td>
<td align="left"><apex:outputText value="{!email.BccAddress}"/></td>
</tr>
</table>
</td>
<td width="30%" align="right">
</td>
</tr>
<tr>
<td width="70%" align="left">
<table cellpadding="2px">
<tr>
<td width="50px" align="right"><b>Subject: </b></td>
<td align="left"><apex:outputText value="{!email.Subject}"/></td>
</tr>
</table>
</td>
<td width="30%" align="right">
<table cellpadding="2px">
<tr>
<td width="80px" align="right"><b>Created by: </b></td>
<td align="left"><i><apex:outputText value="{!email.CreatedBy.Name}"/></i></td>
</tr>
</table>
</td>
</tr>
</table>
<br/>
<br/>
<table>
<tr>
<td width="70%" align="left">
<table cellpadding="2px">
<tr>
<td width="50px" align="right"><b></b></td>
<td align="left"><apex:outputField value="{!email.HtmlBody}"/></td>
</tr>
</table>
</td>
<td width="30%" align="right">
<table cellpadding="2px">
<tr>
<td width="80px" align="right"><b></b></td>
<td align="left"><i></i></td>
</tr>
</table>
</td>
</tr>
</table><br/>
</apex:repeat>
</table>
</apex:component>
And the VisualForce Email Template - 'VF [autoresponse] End-User Auto Closing Email':
<messaging:emailTemplate subject="test" recipientType="Contact" relatedToType="Case">
<messaging:htmlEmailBody >
<html>
<body>
<b><u>Case Emails:</u></b>
<br />
<c:CaseEmailExtension caseId="{!relatedTo.Id}"/>
</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>