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
Vishal BirarisVishal Biraris 

Help - Visual force email Template - Reference to Custom object

Hi all,I am trying to create VF email template. I have account object and inside account I have custom object "Invoices", I need help how we can get "Invoices” object fields into template.

My sample code is –
Messaging:emailTemplate subject="Dunning Reminder" recipientType="Contact" relatedToType="Invoice__c">
<!-- <messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody> -->
 
<messaging:htmlEmailBody >
<b> Dear  {!relatedTo.Name} </b><br/>
Our financial records show that we have not yet received funds for your Virtuagym subscription for the total amount of currency_and_total_amount. <br/>
        <table>
            <tr>
                <th>Invoice Number</th>
            </tr>
           
            <apex:repeat var="cx" value="{!relatedTo.Account_invoice__r}" >
               <tr>
                        <td>{!cx.Invoice_Number__c}</td>
               </tr>
                                   
            </apex:repeat>
        </table>                        
 
I am also getting error – “Error: Invalid field Invoice_Number__r for SObject Account”
Additional Info :-
- Invoice__c : Is API name of my Custom Object Invoices
- Account_invoice__c - Child Relationship Name of Invoices with Accounts
Best Answer chosen by Vishal Biraris
Davy HuijgensDavy Huijgens
If the field is on account just use {!relatedTo.Billing_VAT_Code__c}

All Answers

Davy HuijgensDavy Huijgens
To be able to use a child relationship on Account in your apex:repeat the relatedToType in your template needs to be set to Account instead of Invoice__c.
Vishal BirarisVishal Biraris
After Making suggested changes getting next error (Error: Invalid field Account_invoice__c for SObject Account)




Code changed as per your comment - 
<messaging:emailTemplate subject="Dunning Reminder" recipientType="Contact" relatedToType="Account">
<!-- <messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody> -->

<messaging:htmlEmailBody >
<b> Dear  {!relatedTo.Name} </b><br/>
Our financial records show that we have not yet received funds for your Virtuagym subscription for the total amount of currency_and_total_amount. <br/>
        <table> 
            <tr>
                <th>Invoice Number</th>
            </tr>
            
            <apex:repeat var="cx" value="{!relatedTo.Account_invoice__c}" >
               <tr>
                        <td>{!cx.Account.Invoice_Number__c}</td>
               </tr>
                                   
            </apex:repeat>
        </table>  
                       
Best regards,<br/>
Virtuagym Collection department<br/>
Herengracht 250, 1016 BV Amsterdam, the Netherlands<br/>
www.virtuagym.com<br/>
                
                
</messaging:htmlEmailBody>
</messaging:emailTemplate>
 
Davy HuijgensDavy Huijgens
I do not know all the names for your relations and objects.
 
<apex:repeat var="cx" value="{!relatedTo.Account_invoice__c}" >
    <tr>
       <td>{!cx.Account.Invoice_Number__c}</td>
    </tr>                            
</apex:repeat>

needs to be
 
<apex:repeat var="cx" value="{!relatedTo.Child_Relation__r}" >
    <tr>
       <td>{!cx.Custom_Field_Name__c}</td>
    </tr>                            
</apex:repeat>

 
Vishal BirarisVishal Biraris
Thank you for your reply. Now i am able to sucessfully compile the code but there is no values in Output, i could see only text. please help

Field descriptions - 
Invoice_Number__c -:- is the one of the field in custom object "Invoices"
Invoices__r :- Master-Detail Options - Child Relationship Name (Invoices) , related to (Account)

Code changed - 
<messaging:emailTemplate subject="Dunning Reminder" recipientType="Contact" relatedToType="Account">
<!-- <messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody> -->

<messaging:htmlEmailBody >
<b> Dear  {!relatedTo.Name} </b><br/>
Our financial records show that we have not yet received funds for your subscription. <br/>
        <table> 
            <tr>
                <th>Invoice Number</th>
            </tr>
            
            <apex:repeat var="cx" value="{!relatedTo.Invoices__r}" >
               <tr>
                        <td>{!cx.Invoice_Number__c}</td>
               </tr>
                                   
            </apex:repeat>
        </table>  
                       
Best regards,<br/>
Collection department<br/>
BV Amsterdam, the Netherlands<br/>

                
                
</messaging:htmlEmailBody>
</messaging:emailTemplate>





but after sucessful execution of this code , I am getting below output which has no values displayed.. can you please help 

Dear 
Our financial records show that we have not yet received funds for your subscription. 
Invoice Number
Best regards,
Collection department
BV Amsterdam, the Netherlands
Vishal BirarisVishal Biraris
sorry - My fault..

i can see Output now - 

Dear Lead-21-Aug-2017-1018 
Our financial records show that we have not yet received funds for your subscription. 
Invoice Number
0015E00000N2UEkQAN
0015E00000N2UEkQAN
Best regards,
Collection department
BV Amsterdam, the Netherlands


but i am getting salesforce Id, I want actual Invoice number.
can you please help ?
Davy HuijgensDavy Huijgens
Are you sure there are invoices? 

You could check this by adding text within the repeat:
<apex:repeat var="cx" value="{!relatedTo.Invoices__r}" >
    Test
    <tr>
        <td>{!cx.Invoice_Number__c}</td>
    </tr>
</apex:repeat>
The word Test should be printed in the output for each invoice. 

If Test is printed check field accessability for Invoice_Number__c and if data is actually available. 
 
Davy HuijgensDavy Huijgens
The Id's would suggest you are printing the account lookup field from your invoice object, since they are similar for both invoices and id's starting with 001 are account id's.
Vishal BirarisVishal Biraris
Thanks for your reply .

Output still Coming as  - 
1- 
Dear Vishal-29-Aug-2017-1138 
Our financial records show that we have not yet received funds for your subscription. 
Test
Invoice NumberPayment Due DateAmount DuePayment Reference
0015E00000NWlyfQADTue Aug 29 00:00:00 GMT 2017-54.0128
Best regards,
Collection department
BV Amsterdam, the Netherlands

2- Also date is not coming in Proper format


My code - 
<messaging:emailTemplate subject="Dunning Reminder" recipientType="Contact" relatedToType="Account">
<!-- <messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody> -->

<messaging:htmlEmailBody >
<b> Dear  {!relatedTo.Name} </b><br/>
Our financial records show that we have not yet received funds for your subscription. <br/>
        <table> 
            <tr>
                <th>Invoice Number</th>
                <th>Payment Due Date</th>
                <th>Amount Due</th>
                <th>Payment Reference</th>                                              
            </tr>
            
            <apex:repeat var="cx" value="{!relatedTo.Invoices__r}" >
            Test
               <tr>
                        <td>{!cx.Account_invoice__c}</td>
                        <td>{!cx.Transaction_Due_Date_del__c}</td>
                        <td>{!cx.Amount_FC__c}</td>
                        <td>{!cx.Account_Code__c}</td>                                                                        
               </tr>
                                   
            </apex:repeat>
        </table>  
                       
Best regards,<br/>
Collection department<br/>
BV Amsterdam, the Netherlands<br/>

                
                
</messaging:htmlEmailBody>
</messaging:emailTemplate>




Data into Salesforce

Invoice Number  17700085
Transaction Due Date  29-8-2017
Amount FC  EUR -54,00
Payment Reference  128
Davy HuijgensDavy Huijgens
That is because <td>{!cx.Account_invoice__c}</td> is printing the lookup to account. 

Account_invoice__c is probably the lookup field for account on your invoice__c object. You need to replace it with the field containing your invoice number.

Date formatting is done by using apex:outputtext, more information can be found here:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_outputText.htm

but this is no longer related to your initial question for referencing a custom object. Please select the appropriate answer as the solution to your question.
Vishal BirarisVishal Biraris
Thanks for ur help. 

ur right, I have used incorrect field.
Now output looks okie !
Vishal BirarisVishal Biraris
now last thing related to original question 
If i want to use rhe field from Standard obejct how can add APi field name.

Currently I have added as per below but it's giving me an error - Error: Invalid field Billing_VAT_Code__c for SObject Invoice__c
Billing_VAT_Code__c is the field from Account Object.   





My code is - 
<messaging:emailTemplate subject="Dunning Reminder" recipientType="Contact" relatedToType="Account">
<!-- <messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody> -->

<messaging:htmlEmailBody >
    <STYLE type="text/css">
        TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center }
        TD  {font-size: 11px; font-face: verdana }
        TABLE {border: solid #CCCCCC; border-width: 1}
        TR {border: solid #CCCCCC; border-width: 1}
     </STYLE>
     <font face="arial" size="2">

<b> Dear  {!relatedTo.Name} </b><br/>
<br/>
Our financial records show that we have not yet received funds for your subscription. <br/>
<br/>
        <table border="0" > 
            <tr>
                <th>Invoice Number</th>
                <th>Payment Due Date</th>
                <th>Currency</th>                  
                <th>Amount Due</th>
                <th>Payment Reference</th> 
                <th>Billing VAT Code</th>                                                              
                
            </tr>
            
            <apex:repeat var="cx" value="{!relatedTo.Invoices__r}" >

               <tr>
                        <td>{!cx.Invoice_Number__c}</td>
                        <td>{!cx.Transaction_Due_Date_del__c}</td>
                        <td>{!cx.Currency__c}</td>
                        <td>{!cx.Amount_FC__c}</td>
                        <td>{!cx.Account_Code__c}</td>                                                                        
                        <td>{!cx.Billing_VAT_Code__c}</td>                          
               </tr>
                                   
            </apex:repeat>
        </table>  
     </font>
                    
<br/>
<br/>
<br/>                       
Best regards,<br/>
Collection department<br/>
BV Amsterdam, the Netherlands<br/>

                
                
</messaging:htmlEmailBody>
</messaging:emailTemplate>
Davy HuijgensDavy Huijgens
If the field is on account just use {!relatedTo.Billing_VAT_Code__c}
This was selected as the best answer