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
Albert Zhou 10Albert Zhou 10 

Reference to Custom object history in VF email Template

We have a custom Object "Vendor Contract", We want to send a email alert when the value field in this Object changed.
When doing the VisualForce email template, we want to show the value changed from the OLD Value to New Value.
But when I use "Vendor_Contract__History", it says "Error: Invalid field Vendor_Contract__History for SObject Vendor_Contracts__c"

My Code is down below:
<messaging:emailTemplate subject="Vendor Contract {!relatedTo.Name} requires Approval" 
    recipientType="User" 
    relatedToType="Vendor_Contracts__c">

    <Messaging:htmlEmailBody >
        <html>
            <style type="text/css">
            body {font-family: Calibri; size: 12pt;}
            th {
            font-size: 10pt;
            border: solid;
            border-color: #FF8C00;
            border-width: 0.5px; 
            }
            
            td {
            font-size: 10pt;
            border: solid;
            border-color: #FF8C00;
            border-width: 0.5px;
            text-align: center;
            }
            
            tr {
            border: solid;
            border-width: 20px;
            vertical-align: top;
            text-align: left;
            width: 400px;
            }
            
            </style>
            <body>

            <p>Dear {!recipient.name},</p>
            The following Vendor Contract {!relatedTo.name} has a value change.<br/>
            Below is the information of the Contract.<br/>
            <br/>
            Period: From&nbsp;<apex:outputText value="{0, date, dd MMM yyyy}"><apex:param value="{!relatedTo.Start_Date__c}"/></apex:outputText>&nbsp;To&nbsp;<apex:outputText value="{0, date, dd MMM yyyy}"><apex:param value="{!relatedTo.End_Date__c}"/></apex:outputText><br/>
            <br/>
            Account: {!relatedTo.Account__r.Display_Name__c}<br/>
            <br/>
            Budget: {!relatedTo.Budget__r.Name}<br/>
            <br/>
            Contract Value: &nbsp;<Apex:outputText value="{0, number, currency}"><apex:param value="{!relatedTo.Contract_Value__c}"/></Apex:outputText><br/>
            <br/>
            Budget Remaining: &nbsp;<Apex:outputText value="{0, number, currency}"><apex:param value="{!relatedTo.Estimated_Budget_Remaining__c}"/></Apex:outputText><br/>
            <br/>
            <table border="0" >
                <tr>
                    <th>Description</th><th>New Value</th><th>Old Value</th>
                </tr>
                <apex:repeat var="vcl" value="{!relatedTo.Vendor_Contract__History}">
                <tr>
                    <td width="170">{!vcl.Line_Description__c}</td>
                    <td width="330"> New Value
                    <td width="80"> Old Value
                    </td>
                </tr>
                </apex:repeat> 
            </table>
            <br/>
            You can click the link below to get to the contract detail page to see more details<br/>
            <apex:outputLink Value="{!LEFT($Api.Partner_Server_URL_140,FIND('.com',$Api.Partner_Server_URL_140)+4)+relatedTo.ID}">{!relatedTo.Name}</apex:outputLink><br/>
            

            Kind regards,<br/>
            {!relatedTo.CreatedBy.Name}
                                 
            </body>
        </html>
      
    </Messaging:htmlEmailBody>

</messaging:emailTemplate>


 
Best Answer chosen by Albert Zhou 10
ShirishaShirisha (Salesforce Developers) 
Hi Albert,

Greetings!

Vendor_Contract__History is the Object which was created when you enable the history tracking for the "Vendor_Contracts__c" Object.Seems like you are trying to refer the above Object as field which is why you are getting the error.

I would suggest you to use it in relatedTo Object and then you will be able to call the field which you wanted to show on the email template.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Albert,

Greetings!

Vendor_Contract__History is the Object which was created when you enable the history tracking for the "Vendor_Contracts__c" Object.Seems like you are trying to refer the above Object as field which is why you are getting the error.

I would suggest you to use it in relatedTo Object and then you will be able to call the field which you wanted to show on the email template.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
This was selected as the best answer
Albert Zhou 10Albert Zhou 10
Thanks Shirisha,

I thought History is one of the related list of the object.
Tried this method, and it works.

Many Thanks
Albert
Sanjana RajasekarSanjana Rajasekar
<messaging:emailTemplate recipientType="Contact" relatedToType="Work_Order__c">
    <messaging:HtmlEmailBody>
        <apex:outputPanel rendered="{!relatedTo.Job_Name_Text__c != NULL}">
            <apex:outputLabel value="Job Name: "/>
            <apex:outputText value="{!relatedTo.Job_Name_Text__c}">
                <font style="color:{!IF(relatedTo.Work_Order__c__History[0].OldValue.Job_Name_Text__c != relatedTo.Work_Order__c__History[0].NewValue.Job_Name_Text__c, 'red', 'black')}">
                    {!IF(relatedTo.Work_Order__c__History[0].OldValue.Job_Name_Text__c != relatedTo.Work_Order__c__History[0].NewValue.Job_Name_Text__c, ' (changed from ' + relatedTo.Work_Order__c__History[0].OldValue.Job_Name_Text__c + ' to ' + relatedTo.Work_Order__c__History[0].NewValue.Job_Name_Text__c + ')', '')}
                </font>
            </apex:outputText>
        </apex:outputPanel>
    </messaging:HtmlEmailBody>
</messaging:emailTemplate>
Hi @Albert Zhou 10 , @Shirisha  
I  tried adding relatedTo.Work_Order__c__History. I am facing 
Error: Invalid field Work_Order__c__History for SObject Work_Order__c. Could you help me to find what's wrong ?

Show Less