If workflow is on Case, then it is impossible using Text or HTML emails to show children record merge fields (i.e. Tasks
) because the target object in scope for the email template is the target object of the workflow
To show Tasks - and since there can be more than one Task for a Case, you'll need to think about your business logic as to which task or tasks to show. Once having done that, you will need to:
- Make the email template a VF email template
- Create a custom component to show the related task (or tasks). The custom component uses a custom controller to fetch the related task(s) given a Case.Id that is passed to the component.
VF template
... markup for Hi! ...
<c:taskList caseId={!Case.Id}/> <!-- show tasks here in email body -->
... more markup, if any -- e.g. a footer
VF component
<apex:component access="global" controller="TasksController">
<apex:attribute name="caseId" type="ID" required="true" assignTo={!csId}/>
<apex:repeat value="{!tasks}" var="t"> <!-- can also use dataTable or straight <table> -->
.. markup for a Task row ..
</apex:repeat>
</apex:component>
Component controller
public class TasksController {
public ID csId {get; set;} // gets value before getTasks() is called
public Task[] getTasks() {
return csId != null
? [select id, ... from Task where whatId = :csId ...]
: new List<Task>(); // avoids preview exception
}
}