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
Sanjana RajasekarSanjana Rajasekar 

Reference History in Visualforce Email Template

I have Custom Object "Work_Order__c" . I want to send a email when the value field in this Object changed. How can I use 
Work_Order__c__History in the template.
And If the old value and updated value is not same , I need to highlight in red color.


<messaging:emailTemplate  recipientType="Contact" relatedToType="Work_Order__c">
<messaging:HtmlEmailBody >

<apex:outputPanel rendered="{!relatedTo.Job_Name_Text__c!= NULL}">
    <apex:outputLabel value="Job Name: " /> 
<font 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')}">
    <apex:outputText  value="{!relatedTo.Job_Name_Text__c}" />
    </font>
</apex:outputPanel> 

</messaging:HtmlEmailBody>
</messaging:emailTemplate>
VinayVinay (Salesforce Developers) 
Hi Sanjana,

Relationship name should be 'Histories', can you check below query.
SELECT OldValue, NewValue FROM Histories
Something like below.
{!IF((relatedTo.Histories[0].OldValue.Job_Name_Text__c != relatedTo.Histories[0].NewValue.Job_Name_Text__c), 'red', 'black')}">

Reference:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_query_hist.htm

Please mark as Best Answer if above information was helpful.

Thanks,