function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Email Template with OpportunityLineItems

Hey, can you please help me with this one:

We have a visual force email template with OpportunityLineItems. If that email is sent from Opportunity tab (standard SF email function with 'select template') everything is working fine. What we now would like to have is a similar email template with a couple of Opportunity fields and Opportunity Product Line Items together with some Contract fields in it. This email will be sent from the Contract tab.


This new email template (Contract tab version) is working okay with reference to related Opportunity or Account information like this:

       <td><font face="Arial">
              Description: {!relatedTo.Opportunity__r.Description}<br/> 
              Currency: {!relatedTo.Opportunity__r.CurrencyIsoCode}<br/>                


What would be the right way to have OpportunityLineItems to that email template too? I thought that I would be able to add them via Opportunity__r relationship, but I didn't.


Any comments, advice... help?

You can't traverse relationships up and down like you are seeking to do here.  You can, however, create a custom component with an attribute for the opportunity and a component controller that queries for the line items.  Be careful here though to not stick this component inside an iteration because it will do a query for each opportunity in that case.


thank you for your advice. The thing I was trying to do was refering from child to parent and from there to child again?!

I have to study that custom component possiblity then.


Thanks again,



Right. You can go from the base object (relatedTo) down to it's children and even up from those children to different parents (5 levels). You can also go from the base object up to it's parents (5 levels) but the only downward traversal allowed is from the base down (1 level only) today.


Visualforce and Apex generally follow the rules of relationship traversal in SOQL as described in the API documentation. Here's a good primer:


I thought you weren't allowed to do SOQL within a custom component controller?  That's what it says in the Apex Code Dev Guide (ver 16.0, 27 July 2009, 'Understanding Execution Governors and Limits', footnote 8 on a bunch of the entries in the Governor Limit table). 




Hi Guys,


I'm in the process of trying to come up with a basic VF template that includes Opportunity Line Item information.

Do you by any chance have some sample code I can take a look at to get me started?


Nothing fancy, just need the syntax to get started and work from there.