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
ApolloSApolloS 

Error: Unknown property 'core.email.template.EmailTemplateComponentController.Opportunity'

This is my first attempt at tweaking an APEX. It is a e-mail template for Invoice which is generated from "Send E-mail" within Opportunity. I am getting: " Error: Unknown property 'core.email.template.EmailTemplateComponentController.Opportunity' " when I am trying to save. What am I missing here?

 

 <messaging:emailTemplate subject="Your invoice" recipientType="Contact"  
relatedToType="Opportunity">
<messaging:htmlEmailBody >
 
<table border="0" cellspacing="0" cellpadding="10" width="700" id="table1" align="center">
<tr>
    <td width="230">
        <img src='{!URLFOR($Resource.PixLogo70pix)}' title=" Logo" />
    </td>
    <td align="right"><font face="Arial" size="2" color="#666666"> One Broadway<br />
    </font>
    </td>  
</tr> 
</table>
<center>
    <p style="font-family:Arial;font-size:22px;font-weight:bold;color:#666666;">INVOICE</p>
</center>
<br/>
 
<table border="0" cellspacing="0" cellpadding="5" width="800" id="table1" align="center">
<tr>    <td valign="top">
    <font face="Arial" size="2" color="#666666">
    Bill To:</font><br />
    <font face="Arial" size="2">
    {!Opportunity.Account.Name}<br/>
    {!Opportunity.Account.BillingStreet}<br/>
    {!Opportunity.Account.BillingCity}, {!Opportunity.Account.BillingState} {!Opportunity.Account.BillingPostalCode}<br/>
    {!Opportunity.Account.BillingCountry}<br/>
  </font>
</td> 
        <td>&nbsp;</td>
   <td valign="top">
     <font face="Arial" size="2">
   <font color="#666666">Account Name:</font> <br/>
   {!Opportunity.Account.Name}<br/>
   <font color="#666666">Invoice Number:</font> <br/>
   {!Opportunity.Project_number__c}<br/>
   <font color="#666666">Invoice Date:</font> <br/>
   {!Opportunity.CloseDate}<br />
   <font color="#666666">Payment Terms:</font> <br/>
   <font color="#FF3300">Due Upon Receipt</font></font></td>    
</tr>
</table>
<br/>
 
<table width="800" border="0" align="center" cellpadding="3" cellspacing="1" id="table4">
<tr bgcolor="#E5E5E5"><font face="Arial" size="2" color="#666666">
       <td>Product</td>
      <td>Description</td>
       <td>Quantity</td>
       <td>Unit Price</td>
       <td>Total Price</td>
       </font>
</tr>
<tr><font face="Arial" size="2" color="#000000">
       <apex:repeat value="{!Opportunity.OpportunityLineItems}" var="line">
          <tr>
             <td>{!line.PricebookEntry.Name}</td>
             <td>{!line.Description}</td>           
             <td>{!line.Quantity}</td>
             <td><apex:OutputField value="{!line.UnitPrice}"/></td>
             <td><apex:OutputField value="{!line.TotalPrice}"/></td>
          </tr>
       </apex:repeat>  
       </font>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial" size="2">Sub-total:&nbsp;<apex:OutputField value="{!Opportunity.Sub_Total__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial" size="2">Discounts:&nbsp;<apex:OutputField value="{!Opportunity.Discount_amount__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial">Tax:&nbsp;<apex:OutputField value="{!Opportunity.Sales_Tax__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#E5E5E5" align="right" colspan="5">
       <font face="Arial"><b>Total:</b>&nbsp;<apex:OutputField value="{!Opportunity.Amount}"/></font></td>
</tr>
</table>
<br/>
 
<p align="center"><font face="Arial">Thank you very much for you business!<br />
{!Opportunity.CreatedBy.FirstName} {!Opportunity.CreatedBy.LastName}
</font></p>
</messaging:htmlEmailBody>
 
<messaging:attachment renderAs="pdf" filename="{!relatedTo.name}">  
<table border="0" cellspacing="0" cellpadding="10" width="700" id="table1" align="center">
<tr>
    <td width="230">
        <img src='{!URLFOR($Resource.PixLogo70pix)}' title="Pixability Logo" />
    </td>
    <td align="right"><font face="Arial" size="2" color="#666666"> One Broadway<br />
</font>
    </td>  
</tr>
</table>
<center>
    <p style="font-family:Arial;font-size:22px;font-weight:bold;color:#666666;">INVOICE</p>
</center>
<br/>
 
<table border="0" cellspacing="0" cellpadding="5" width="800" id="table1" align="center">
<tr>    <td valign="top">
    <font face="Arial" size="2" color="#666666">
    Bill To:</font><br />
    <font face="Arial" size="2">
    {!Opportunity.Account.Name}<br/>
    {!Opportunity.Account.BillingStreet}<br/>
    {!Opportunity.Account.BillingCity}, {!Opportunity.Account.BillingState} {!Opportunity.Account.BillingPostalCode}<br/>
    {!Opportunity.Account.BillingCountry}<br/>
  </font>
</td> 
        <td>&nbsp;</td>
   <td valign="top">
     <font face="Arial" size="2">
   <font color="#666666">Account Name:</font> <br/>
   {!Opportunity.Account.Name}<br/>
   <font color="#666666">Invoice Number:</font> <br/>
   {!Opportunity.Project_number__c}<br/>
   <font color="#666666">Invoice Date:</font> <br/>
   {!Opportunity.CloseDate}<br />
   <font color="#666666">Payment Terms:</font> <br/>
   <font color="#FF3300">Due Upon Receipt</font></font></td>    
</tr>
</table>
<br/>
 
<table width="800" border="0" align="center" cellpadding="3" cellspacing="1" id="table4">
<tr bgcolor="#E5E5E5"><font face="Arial" size="2" color="#666666">
       <td>Product</td>
      <td>Description</td>
       <td>Quantity</td>
       <td>Unit Price</td>
       <td>Total Price</td>
       </font>
</tr>
<tr><font face="Arial" size="2" color="#000000">
       <apex:repeat value="{!Opportunity.OpportunityLineItems}" var="line">
          <tr>
             <td>{!line.PricebookEntry.Name}</td>
             <td>{!line.Description}</td>           
             <td>{!line.Quantity}</td>
             <td><apex:OutputField value="{!line.UnitPrice}"/></td>
             <td><apex:OutputField value="{!line.TotalPrice}"/></td>
          </tr>
       </apex:repeat>  
       </font>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial" size="2">Sub-total:&nbsp;<apex:OutputField value="{!Opportunity.Sub_Total__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial" size="2">Discounts:&nbsp;<apex:OutputField value="{!Opportunity.Discount_amount__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial">Tax:&nbsp;<apex:OutputField value="{!Opportunity.Sales_Tax__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#E5E5E5" align="right" colspan="5">
       <font face="Arial"><b>Total:</b>&nbsp;<apex:OutputField value="{!Opportunity.Amount}"/></font></td>
</tr>
</table>
<br/>
 
<p align="center"><font face="Arial">Thank you very much for you business!<br />
{!Opportunity.CreatedBy.FirstName} {!Opportunity.CreatedBy.LastName}
</font></p>
</messaging:attachment>      
</messaging:emailTemplate>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
icemft1976icemft1976

You correctly bound the 'relatedtotype' at the top of your template....but inside the template try replacing "Opportunity" in the email body with {!relatedTo}. Templates use this generic placeholder for whatever Sobject type you choose to associate with them

 

{!Opportunity.Account.Name}  ->  {!relatedTo.Account.Name}

{!Opportunity.OpportunityLineItems}  ->  {!relatedTo.OpportunityLineItems}

  etc...

 

check out the example from the docs, at the bottom it shows how to use 'relatedTo' (and also how child objects are referenced)

 

 

 

All Answers

icemft1976icemft1976

You correctly bound the 'relatedtotype' at the top of your template....but inside the template try replacing "Opportunity" in the email body with {!relatedTo}. Templates use this generic placeholder for whatever Sobject type you choose to associate with them

 

{!Opportunity.Account.Name}  ->  {!relatedTo.Account.Name}

{!Opportunity.OpportunityLineItems}  ->  {!relatedTo.OpportunityLineItems}

  etc...

 

check out the example from the docs, at the bottom it shows how to use 'relatedTo' (and also how child objects are referenced)

 

 

 

This was selected as the best answer
ApolloSApolloS

Thank you for pointing it out to me. This is my first template (and I am not exactly a programmer).

 

I am finally able to save it, but I am now getting PDF generation error:

 

Error occurred trying to load the template for preview: PDF generation failed. Check the page markup is valid.. Please try editing your markup to correct the problem.

 

Here is the code I fixed with your help. I wonder if the OpportunityItems or Sub-total, Shipping, or Tax area is screwing it up for me.

Visualforce is still bit over my head for now.

 

 

<messaging:emailTemplate subject="Your invoice" recipientType="Contact"  
relatedToType="Opportunity">
<messaging:htmlEmailBody >

<table border="0" cellspacing="0" cellpadding="10" width="700" id="table1" align="center">
<tr>
    <td width="230">
        <img src='{!URLFOR($Resource.PixLogo70pix)}' title=" Logo" />
    </td>
    <td align="right"><font face="Arial" size="2" color="#666666"> One Broadway<br />
    Floor<br />
    City, MA. 02142<br />
</font>
    </td>  
</tr> 
</table>
<center>
    <p style="font-family:Arial;font-size:22px;font-weight:bold;color:#666666;">INVOICE</p>
</center>
<br/>

<table border="0" cellspacing="0" cellpadding="5" width="800" id="table1" align="center">
<tr>    <td valign="top">
    <font face="Arial" size="2" color="#666666">
    Bill To:</font><br />
    <font face="Arial" size="2">
    {!relatedTo.Account.Name}<br/>
    {!relatedTo.Account.BillingStreet}<br/>
    {!relatedTo.Account.BillingCity}, {!relatedTo.Account.BillingState} {!relatedTo.Account.BillingPostalCode}<br/>
    {!relatedTo.Account.BillingCountry}<br/>
  </font>
</td> 
        <td>&nbsp;</td>
   <td valign="top">
     <font face="Arial" size="2">
   <font color="#666666">Account Name:</font> <br/>
   {!relatedTo.Account.Name}<br/>
   <font color="#666666">Invoice Number:</font> <br/>
   {!relatedTo.Project_number__c}<br/>
   <font color="#666666">Invoice Date:</font> <br/>
   {!relatedTo.CloseDate}<br />
   <font color="#666666">Payment Terms:</font> <br/>
   <font color="#FF3300">Due Upon Receipt</font></font></td>    
</tr>
</table>
<br/>

<table width="800" border="0" align="center" cellpadding="3" cellspacing="1" id="table4">
<tr bgcolor="#E5E5E5"><font face="Arial" size="2" color="#666666">
       <td>Product</td>
      <td>Description</td>
       <td>Quantity</td>
       <td>Unit Price</td>
       <td>Total Price</td>
       </font>
</tr>
<tr><font face="Arial" size="2" color="#000000">
       <apex:repeat value="{!relatedTo.OpportunityLineItems}" var="line">
          <tr>
             <td>{!line.PricebookEntry.Name}</td>
             <td>{!line.Description}</td>           
             <td>{!line.Quantity}</td>
             <td><apex:OutputField value="{!line.UnitPrice}"/></td>
             <td><apex:OutputField value="{!line.TotalPrice}"/></td>
          </tr>
       </apex:repeat>  
       </font>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial" size="2">Sub-total:&nbsp;<apex:OutputField value="{!relatedTo.Sub_Total__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial" size="2">Discounts:&nbsp;<apex:OutputField value="{!relatedTo.Discount_amount__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial">Tax:&nbsp;<apex:OutputField value="{!relatedTo.Sales_Tax__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#E5E5E5" align="right" colspan="5">
       <font face="Arial"><b>Total:</b>&nbsp;<apex:OutputField value="{!relatedTo.Amount}"/></font></td>
</tr>
</table>
<br/>

<p align="center"><font face="Arial">Thank you very much for you business!<br />
{!relatedTo.CreatedBy.FirstName} {!relatedTo.CreatedBy.LastName}
</font></p>


</messaging:htmlEmailBody>

    <messaging:attachment renderAs="pdf" filename="{!relatedTo.name}">

<table border="0" cellspacing="0" cellpadding="10" width="700" id="table1" align="center">
<tr>
    <td width="230">
        <img src='{!URLFOR($Resource.PixLogo70pix)}' title="Logo" />
    </td>
    <td align="right"><font face="Arial" size="2" color="#666666"> One Broadway<br />
    Floor<br />
    City, MA. 02142<br />
</font>
    </td>  
</tr> 
</table>
<center>
    <p style="font-family:Arial;font-size:22px;font-weight:bold;color:#666666;">INVOICE</p>
</center>
<br/>

<table border="0" cellspacing="0" cellpadding="5" width="800" id="table1" align="center">
<tr>    <td valign="top">
    <font face="Arial" size="2" color="#666666">
    Bill To:</font><br />
    <font face="Arial" size="2">
    {!relatedTo.Account.Name}<br/>
    {!relatedTo.Account.BillingStreet}<br/>
    {!relatedTo.Account.BillingCity}, {!relatedTo.Account.BillingState} {!relatedTo.Account.BillingPostalCode}<br/>
    {!relatedTo.Account.BillingCountry}<br/>
  </font>
</td> 
        <td>&nbsp;</td>
   <td valign="top">
     <font face="Arial" size="2">
   <font color="#666666">Account Name:</font> <br/>
   {!relatedTo.Account.Name}<br/>
   <font color="#666666">Invoice Number:</font> <br/>
   {!relatedTo.Project_number__c}<br/>
   <font color="#666666">Invoice Date:</font> <br/>
   {!relatedTo.CloseDate}<br />
   <font color="#666666">Payment Terms:</font> <br/>
   <font color="#FF3300">Due Upon Receipt</font></font></td>    
</tr>
</table>
<br/>

<table width="800" border="0" align="center" cellpadding="3" cellspacing="1" id="table4">
<tr bgcolor="#E5E5E5"><font face="Arial" size="2" color="#666666">
       <td>Product</td>
      <td>Description</td>
       <td>Quantity</td>
       <td>Unit Price</td>
       <td>Total Price</td>
       </font>
</tr>
<tr><font face="Arial" size="2" color="#000000">
       <apex:repeat value="{!relatedTo.OpportunityLineItems}" var="line">
          <tr>
             <td>{!line.PricebookEntry.Name}</td>
             <td>{!line.Description}</td>           
             <td>{!line.Quantity}</td>
             <td><apex:OutputField value="{!line.UnitPrice}"/></td>
             <td><apex:OutputField value="{!line.TotalPrice}"/></td>
          </tr>
       </apex:repeat>  
       </font>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial" size="2">Sub-total:&nbsp;<apex:OutputField value="{!relatedTo.Sub_Total__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial" size="2">Discounts:&nbsp;<apex:OutputField value="{!relatedTo.Discount_amount__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#FFFFFF" align="right" colspan="5">
       <font face="Arial">Tax:&nbsp;<apex:OutputField value="{!relatedTo.Sales_Tax__c}"/></font></td>
</tr>
<tr>
       <td bgcolor="#E5E5E5" align="right" colspan="5">
       <font face="Arial"><b>Total:</b>&nbsp;<apex:OutputField value="{!relatedTo.Amount}"/></font></td>
</tr>
</table>
<br/>

<p align="center"><font face="Arial">Thank you very much for you business!<br />
{!relatedTo.CreatedBy.FirstName} {!relatedTo.CreatedBy.LastName}
</font></p>

    </messaging:attachment>      
    

</messaging:emailTemplate>

 

 

icemft1976icemft1976

hi,

 

I think you almost have it. If it's just a markup error then it sounds like you're missing some standard html markup.

 

Looking at the example docs again, (about half way down, under 'Changing the renderAs Attribute')  it looks like they want a full html page declared? Might be it

 

try this:

 

<messaging:attachment renderAs="pdf" filename="{!relatedTo.name}">
<html>
<body>


<table border="0" cellspacing="0" cellpadding="10" width="700" id="table1" align="center">
<tr>
<td width="230">
.
.
.
<p align="center"><font face="Arial">Thank you very much for you business!<br />
{!relatedTo.CreatedBy.FirstName} {!relatedTo.CreatedBy.LastName}
</font></p>

</body>
</html>


</messaging:attachment>

 

 

 

 

 

ApolloSApolloS

Worked like a charm! Thank you!

 

The only problem I have right now is that <font></font> is being ignored by the PDF generator. I'd like to put everything in Arial there. Any way to do it or is it something within Salesforce that limits that?

icemft1976icemft1976

I believe that has to do with how PDFs handle styling. I've had issues wiht 'inline' styling like you are using. I reverted to using Style Classes ....since you're reusing the same styles, this would be easier way to manage the look and feel anyway.

 

two pages have tips about PDFs & salesforce -they both avoid the inline styling...but I'm not sure if that's the issue.

 

 

Rendering a Page as a PDF

Best Practices for Rendering PDFs.

 

 You might also want to look at not using relative sizing (ie instead of size="2" do "14px" ) because pdf is for print...it might prefer it.