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
Swapnil PatneSwapnil Patne 

Trying to create a visualforce email template that shows information from opportunity field history i.e. "Changed Amount from xxx to xxx"

I am trying to create a simple visualforce email template that shows information from opportunity and opportunity field history, but struggling with the history fields.. Please can someone help me with the code?

The email will say..

Please note the revenue on this opportunity was updated. Changed Amount from xxx (old value) to xxx (new value).

Account name:  {!Account.Name}

Opportunity type: {!Opportunity.Type}

Opportunity owner: {!Opportunity.OwnerFullName}

Opportunity Name: {!Opportunity.Name}

Closed Date: {!Opportunity.CloseDate}

Total Amount: {!Opportunity.Total_Amount__c}

To see the old value, please click here- {!Opportunity.Link}

Many thanks,
Swapnil
Best Answer chosen by Swapnil Patne
Andy BoettcherAndy Boettcher
Swapnil,

The Opportunity History object contains the values you are looking for.  Example code:

<apex:repeat value="{!relatedTo.OpportunityHistories}" var="history">
    On {!history.CreatedDate}, Amount was changed to {!history.Amount}.
</apex:repeat>

All Answers

Andy BoettcherAndy Boettcher
Swapnil,

The Opportunity History object contains the values you are looking for.  Example code:

<apex:repeat value="{!relatedTo.OpportunityHistories}" var="history">
    On {!history.CreatedDate}, Amount was changed to {!history.Amount}.
</apex:repeat>
This was selected as the best answer
Swapnil PatneSwapnil Patne
Hi Andy,

Thank you for your reply.

I placed the code inside the email body and tried to save it, but it throws an error message: "Invalid field OpportunityHistory for SObject OpportunityHistory"

<messaging:emailTemplate subject="Change of Opportunity Value Notification" recipientType="User" relatedToType="OpportunityHistory">
<messaging:plainTextEmailBody >
<apex:repeat value="{!relatedTo.OpportunityHistory}" var="history">
    On {!history.CreatedDate}, Amount was changed to {!history.Amount}.
</apex:repeat> 
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

Swapnil
Andy BoettcherAndy Boettcher
Sounds like your template is hooked to the OpportunityHistory object - switch that to Opportunity.  (Posting your code in question would be highly beneficial for future posts.  *smile*)
Swapnil PatneSwapnil Patne
Perfect! that resolved the error. But how do I include these fields from opportunbity object, would you be abel to help me with this?

Account name:  {!Account.Name}

Opportunity type: {!Opportunity.Type}

Opportunity owner: {!Opportunity.OwnerFullName}

Opportunity Name: {!Opportunity.Name}

Closed Date: {!Opportunity.CloseDate}

Total Amount: {!Opportunity.Total_Amount__c}

Swapnil
Andy BoettcherAndy Boettcher
In your merge fields, change opportunity to "relatedto"
Swapnil PatneSwapnil Patne
Okay, perfect! that worked for me. Many thanks Andy.

Now trying to figure out how to get this formula working-  https://success.salesforce.com/answers?id=90630000000gqdbAAA (only if you can help)

Swapnil