Vandevelde Jan

Certified Salesforce Consultant || Salesforce MVP

For Admins

The not-so-scary Visualforce Email Template as Order Confirmation Email


Almost every Salesforce admin knows how to create an email template which can then be used in Workflow Rules, Process Builder, Visual Flow or just manually from the “Send email” button in the activity history related list.

Actually when creating an email template you’ve got 4 options:

  • Text
  • HTML (with a letterhead)
  • Custom (without using a letterhead)
  • Visualforce

The first 3 options let you create email templates containing mergefields of a particular object and fields of lookup relationships going up, but not down.

In most cases that’s all you need. But what if you would like to create an email template containing records from a related list like for example an Order Confirmation Email, containing the Order Details and a table of it’s related products ?

Most beginning admins don’t even want to touch anything that says Visualforce. It scares a lot of them because it’s linked to code. Also some admins think they can’t create Visualforce Email templates because they think it’s not available in their Salesforce edition. And that actually is a misconception.

The use of Visualforce Email Templates is available in: Contact Manager, Group, Professional, Enterprise, Performance, Unlimited, and Developer Editions and it’s able to solve a very common business scenario!

The following problem was posted on the Salesforce Success Community – Getting Started by Elin Danielsen

Ok… I’m stuck. :)

And on something I thought would be simple and straightforward, so it is quite likely I’m simply just not seeing the right way to do this…


I need to make an Order Confirmation Email. In that email it is vital that we list the product names and details of the products purchased by the customer. Something like this:

Order #: {!Order.OrderNumber}




Your order total: {!Order.TotalAmount} {!Order.CurrencyIsoCode}


I cannot get Product name from the Order object. There *is* an OrderProduct object. However, 1) I cannot pull fields from this into my template (cross-object) directly, and 2) this object does not contain the product name. There is of course the Product object, which does have the name, but again: I cannot pull this directly into the email template.

Possible solution and questions:

I have already created a junction object between Cases and Assets in order to be able to add more than one asset to a case, so I know that that is possible and how to do it (in the abstract). The question is: is this what I have to do in order to be able to create a complete Order Confirmation Template? How would you configure the junction (which object to be masters)? I presume Order = primary master, Product = secondary master and OrderHasProducts (or similar) as junction object? If this is the case, can I not somehow use the already existing OrderProducts object for some good here?

Procedure I used previously to make a junction object:

And lastly:

Am I simply overcomplicating this? Is there some built-in way to send an order confirmation email that I am just not seeing….?

I’d be grateful for any input. :)

Actually this is a common business scenario and having studied for 3 certification exams already, I haven’t come across the use of setting up a VisualForce Email template in any of the online courses. That’s a real shame!

So I get that not everybody knows that a VisualForce Email template offers THE SOLUTION to this common problem!

That’s why I decided to post an example of how to set up a Visualforce Email template as an Order Confirmation Email with its related products included.

The Output:

The Visualforce code:


Code explanation: 

See? Not that scary anymore isn’t it!

After sending some messages back and forth between us Elin’s reaction was:

Elin Danielsen (Customer)

???? It’s all working splendidly now! Started to get familiar with the Salesforce data model :) Now I am of course getting more ambitious, and would like lines to disappear if there are no values (like…if there is no “Software contact” for this order, then don’t show the line for that in the email). This was quite a lot of fun. I will have to ask my boss to be sent on a programming course ???? Thank you again so much!

Jan Vandevelde

What you want to do is possible like for example if I wanted to show the following text when the ordertype is structural I would place my text within apex:outputText component and use the render attribute (condition) to say when to show it or not like this:<apex:outputText value=”Order type: {!relatedTo.Type}” rendered=”{!relatedTo.Type == ‘Structural’}” />I just tested this and works fine. If I test the email with an order where order type is structural the text is shown and otherwise when it’s One Time or something else, it won’t show in the email.

Elin Danielsen (Customer)

hehe See, the problem is that now I want to put my other work aside to try this!

This is just a faboulous example why the Salesforce Success Community is soo powerfull! Salesforce customers, employees, mvp’s and developers help eachother out where possible!

I’m glad to have helped out Elin and I’m also very glad she didn’t shy away from words like Visualforce or Apex. Now she’s going to have a new hobby and it reminds me of me some months ago ????

You really get to love the platform, it’s language/syntax because of it’s simplicity and flexibility!

Here are some of the greatest resources I found when I started creating my first Visualforce Email Template:


# Derek 2016-04-15 08:57
Great post! The code explanation is very helpful.

Are you familiar with any methods to build a table with fields from the same record? Thanks!
Reply | Reply with quote | Quote
# Ninie 2016-06-27 14:37
Hi, Thank you for sharing. Do you any idea how to create a table with list of pending records from Case object.

Kindly advice. Thanks!
Reply | Reply with quote | Quote
# Nata 2016-09-20 01:04
Very useful !! Thank you!
Reply | Reply with quote | Quote
# Drew 2016-10-28 15:40
Thanks! Fit my use case to a tee! We needed to show related parent-child cases in the email template. This made it a no-brainer.
Reply | Reply with quote | Quote
# Denni 2016-10-28 18:34
Can you send me the code?
Reply | Reply with quote | Quote
# Mark 2016-12-02 18:30
My requirement we more simple - not a relation of a relation like yours - but your explanation and reasoning was crystal clear - thank you so much.

So many these APEX 'coding heroes' write flawed/broken code that it's impossible to get to grips with a new a new concept - but your code works - thanks again.
Reply | Reply with quote | Quote
# Kevin 2018-04-19 21:36
Great example - however this is just a screenshot of visualforce code. Completely useless for folks who would like to copy and paste etc.... :-)

Just saying....
Reply | Reply with quote | Quote

Add comment

Security code

On Twitter

Contact Info

Jan Vandevelde

Ninoofsesteenweg 42

9450 Denderhoutem


Mobile: +32 (0) 472 58 47 67


Email Address:

VAT BE 0882 025 057