+ Start a Discussion

How to send a case solution using an email template

Currently I have a template with some merge fields that are populated with solutions and case records' values. It works fine when I have only one solution, but in some cases I send a solution to the contact that is not valid, so I select a new solution and leave previous one to have the history of solutions. When I select again the template both solutions are included in the email, so I have to delete first one manually. Is there any way to include only last one solution using standard templates? Or will I have always all the solutions included?
No, unfortunately the merge field will include all the solutions on the case.

With the arrival of Winter09 and visualforce email templates I've got to create an email template that includes only one solution. It looks like this:

<messaging:emailTemplate subject="Solution delivery" recipientType="Contact" relatedToType="Case">
<messaging:htmlEmailBody >
<p>Hello {!recipient.name}--</p>
<p>This is the solution provided for your case:</p>
<p>Case Number: {!relatedTo.CaseNumber}--</p>
<p>Subject: {!relatedTo.Subject}--</p>
<p>Creator's Email: {!relatedTo.Contact.email}--</p>
<p>Status: {!relatedTo.Status}--</p>
<apex:repeat first="1" rows="1" value="{!relatedTo.CaseSolutions}" var="sol">
<apex:outputText value="{!sol.Solution.SolutionName}" escape="false"/><br/></p>
<apex:outputText value="{!sol.Solution.SolutionNote}" escape="false"/><br/></p>


The interesting part is marked in bold. This template always sends the first solution added to the case. If I want to send the second one I would have to create another template with the following:


<apex:repeat first="2" rows="1" value="{!relatedTo.CaseSolutions}" var="sol">

I would like to be able to pass as parameter the number of solution I want to send or be able to send only the last solution. Maybe I'm missing something, but I can't find the way, is it possible to write code in a visualforce email template (a class controller) as I can read in Winter09 Salesforce's post?


Create Rich Email Templates

(Group, Professional, Enterprise, Unlimited, and Developer Editions)

  • Build dynamic email templates with conditions
  • Pull information from anywhere within your application
  • Create documents and attach them to emails

When Visualforce was released in Summer ’08, it provided unprecedented power and flexibility to customize the Salesforce CRM user interface to meet the needs of any application. Now, in Winter ’0 9, that same power and flexibility is available to create richly formatted email templates.

Using the same HTML-like tags and Apex controller concepts as Visualforce pages, developers can create email templates that display information from multiple objects, with precision control over format and styling. For example, you can create a sales quote email template to send to a prospect that includes information from the opportunity object, but also from opportunity line items as well as warranty information about each product. Plus, you can dynamically create personalized PDF or text documents and deliver them via email.

Apex controller logic lets you dynamically change the content and formatting of email templates based on rules you define. Want to highlight certain fields or display additional information based on the status of a case or the number of days it has been open? Visualforce email templates give you the power to do so.


From my experience you can create a component that renders the output you want and then put that into your VF email template.


This is all example code, and will need to be refined for your solution


The VF template:

<c:newComponent id="{!RelatedTo.Id}" />


The component:

<apex:component controller="newController" access="global">
<apex:attribute name="id" type="id" assignTo="{!recordId}" description="id of the parent record" />
<apex:repeat first="{!rowsToDisplay}" value="{!childRecords}" var="child">
<apex:outputText value="{!child.field}" 


The controller:

public class newController{
sObject[] children;
public sObject[] childRecords(){
  return children;
Id pRecordId;
public Id getRecordId(){return pRecordId;}
public Id setRecordId(Id recId){
 children = [SELECT Id FROM Somewhere WHERE ParentId = :recId];