+ Start a Discussion
Emily Jones 4Emily Jones 4 

Visual Force email template want to show only last activities history related to Opportunity

Hi,
I have created a Visual force email template related to opportunity. I want to show last 2 activities realted to opportunity in this email template.
I used <apex:repet rows="4" and it is showing 4 record but accending order. I want to show last activity first then second last activity.

here is my code:

<messaging:emailTemplate subject="Stage  change reminder" recipientType="User" relatedToType="Opportunity">
 <messaging:htmlEmailBody >
  <html>
    <body>
        <h1 style="font-size:100%">
        <p>Dear {!recipient.name},</p></h1>
      
        <p>FYI, appended below is a snapshot of the last few activities related to this opportunity:</p>
            <table border="3" style="width:100%">
             
                <tr>
                    <th>View Activity</th>
                    <th>ActivityDate</th>
                    <th>Subject</th>
                    <th>Status</th>
                </tr>
          
                <apex:repeat rows="2" var="cx" value="{!relatedTo.ActivityHistories}">
                <tr>
                    <td><a href = "https://ap2.salesforce.com/{!cx.id}">View</a></td>
                    <td>{!cx.ActivityDate}</td>
                    <td>{!cx.Subject}</td>
                    <td>{!cx.Status}</td>
                </tr>
                </apex:repeat>
            </table>
            
            <p> Thank You</p>
            <p> Salesforce Team </p>
           
    </body>

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

Plz help me to show only last 4 activities in decending order.
 
krishna chaitanya 35krishna chaitanya 35
Hi Emily,
1.please create a component using controller for retrieving recent activities through soql.
2.Add that component to the visualforce template.
please refer the following link:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm
Emily Jones 4Emily Jones 4
Hi Krish ( Sorry to short ur name),
I have created custom controller:
public  class oppTemplt
{

    public String getOpptys() {
        return null;
    }

    public Id accountId {get;set;}
    List<Opportunity> oppty;
    public opptTemplt(){}
    public List<Opportunity> getoppty()
    {
        oppty = [SELECT id, (SELECT ActivityDate,Subject, Status, Description FROM ActivityHistories) FROM Opportunity ORDER BY CreatedDate ASC LIMIT 4];
        return oppty ;    
}

}

and componant

<apex:component controller="opptTemplt" access="global">
    <apex:attribute name="AccountId" type="Id" description="Id of the account" assignTo="{!accountId}"/>
    <table border = "2" cellspacing = "5">
        <tr>
            <td>Opportunity Name</td>
            <td>Opportunity Stage</td>               
        </tr>
        <apex:repeat value="{!opptys.ActivityHistories}" var="cx">
        <tr>
             <tr>
                    <td><a href = "https://ap2.salesforce.com/{!cx.id}">View</a></td>
                    <td>{!cx.ActivityDate}</td>
                    <td>{!cx.Subject}</td>
                    <td>{!cx.Status}</td>
                </tr>        
        </tr>
        </apex:repeat>       
    </table>
</apex:component>

But when I tried to save it. It was showing quickfix error and when i clicked on quickfix it was giving unknown error.
Please let me know where i was doing wrong.

Thanks
Emily
krishna chaitanya 35krishna chaitanya 35
public with sharing class MyController {
    private Id oppId;
    public MyController(ApexPages.StandardController sc) {
        oppId = sc.getId();
    }

    public Task[] getTasks() {
        if (accountId != null) {
            return [
            
                    select Id, Subject,activitydate from Task where WhatId in (select Id from Opportunity where = :oppId)  order by activitydate  desc
             
                    ];
        } else {
            return new Task[] {};
        }
    }
}