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 four options:

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

The first 3 options let you create email templates containing merge fields 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 its related products ?

Most beginning administrators 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 is actually 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

Visualforce Email Template

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 related products included.

The Output:

The Visualforce code:

VFcode sample confirmation email1

Code explanation: 

See? It’s 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 (Me)

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)

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

This is just a fabulous example of why the Salesforce Success Community is soo powerful! Salesforce customers, employees, mvp’s and developers help each other 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, and its language/syntax because of their simplicity and flexibility!

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



Table of Contents

Continue Reading

Related Posts

Industry Best Practices

Why Ignoring Industry Best Practices Could End Your Consulting Career

Learn why ignoring industry best practices in the consulting industry can have detrimental consequences for your career. From attending conferences and subscribing to industry journals to networking with peers and taking specialized courses, discover the essential steps to stay informed and relevant in your field. Don’t underestimate the importance of industry best practices – they could be the key to securing a successful and thriving consulting career.