+ Start a Discussion
Russell baker 1Russell baker 1 

VF email template want to show only last 2 activities 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.
But as per my code it is showing all activities related to opportunty.
How can i restrict number of avtivities show on the page.

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 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 2 activities.

Regards
Russell
Best Answer chosen by Russell baker 1
Russell baker 1Russell baker 1
The correct answer
Controller class:
public class OpportunityActivities {
    public Id oppId {
       get;
        set;
    }

    public OpportunityActivities () {}

    public List<Opportunity> getoppty() {
        return [   
            select 
                (
                    select ActivityDate,
                        Subject,
                        Status,
                        Description
                    from ActivityHistories where WhatId =: this.oppId order by CreatedDate DESC
            limit 2

                )
            from Opportunity
                    ]; 
    }
}
Visualforce componant :
 
<apex:component controller="OpportunityActivities" access="global">
    <apex:attribute name="OpportunityId" type="Id" description="Id of the opportunity" assignTo="{!oppId}" />

    <table border="2" cellspacing="5">
        <tr>
            <td>Action</td>
            <td>Activity Date</td>
            <td>Activity Subject</td>
            <td>Activity Status</td>
        </tr>
        <apex:repeat value="{!oppty}" var="opp"> 
        <apex:repeat value="{!opp.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>
         </apex:repeat>
    </table>
</apex:component>

and email template:
<messaging:emailTemplate subject="List of opportunity" recipientType="User" relatedToType="opportunity">
    <messaging:htmlEmailBody >
   <p>Hi {!relatedTo.owner.name},</p>
        <p>Please be informed {!relatedTo.name} is nearing the stage duration planned timeline of 14 days tomorrow.</p>

        <p>Kindly revert back with your plan for us to have the same updated in Salesforce.</p>

        <p>FYI, appended below is a snapshot of the last few activities related to this opportunity:</p>   
        <c:oppacct OpportunityId="{!relatedTo.Id}" /><br/><br/>
    <b>Regards,</b><br/>
    {!recipient.FirstName}
    </messaging:htmlEmailBody>
</messaging:emailTemplate>

Regards
Russell
 

All Answers

Prabhat Kumar12Prabhat Kumar12
Hi Russell,

There is no standard way to do this, but you can achieve this by using custom controller in visualforce page.

Create a controller.
 
public  class acctTemplt
{
    public Id accountId {get;set;}
    public List<Opportunity> getopptys()
    {
        List<Opportunity> oppty;
        oppty = [SELECT id, (SELECT ActivityDate,Subject, Status, Description FROM ActivityHistories) FROM Opportunity ORDER BY CreatedDate ASC LIMIT 2];
        return oppty;
    }
}
Component:

Name:OpptyList​
<apex:component controller="acctTemplt" access="global">
    <apex:attribute name="AcctId" 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>
Now use the componect in template. Something like this.
 
<messaging:emailTemplate subject="List of opportunity" recipientType="User" relatedToType="
opportunity">
    <messaging:htmlEmailBody >
    Hi,<br/>
    Below is the list of opportunities for your account {!relatedTo.Name}.<br/><br/>
   <c:OpptyList AcctId="{!relatedTo.Id}" /><br/><br/>
    <b>Regards,</b><br/>
    {!recipient.FirstName}
    </messaging:htmlEmailBody>
</messaging:emailTemplate>

You need make changes as per your requirment.

Thanks.

 
Russell baker 1Russell baker 1
Hi Prabhat,

Thank you so much for your reply. I'll try this but i got one solution which is working fine for me:

<apex:repeat rows="2" var="cx" value="{!relatedTo.ActivityHistories}">

It is showing last 2 activities in my email template.

Hope if any flaw in this thing you will let me know.

Regards
Russell

 
Russell baker 1Russell baker 1
Hi Prabhat,

when I tried your code it is showing errors.
User-added image

Please help me out. It's very urgent.

regards
Russell
Russell baker 1Russell baker 1
Any one plz help..
Russell baker 1Russell baker 1
The correct answer
Controller class:
public class OpportunityActivities {
    public Id oppId {
       get;
        set;
    }

    public OpportunityActivities () {}

    public List<Opportunity> getoppty() {
        return [   
            select 
                (
                    select ActivityDate,
                        Subject,
                        Status,
                        Description
                    from ActivityHistories where WhatId =: this.oppId order by CreatedDate DESC
            limit 2

                )
            from Opportunity
                    ]; 
    }
}
Visualforce componant :
 
<apex:component controller="OpportunityActivities" access="global">
    <apex:attribute name="OpportunityId" type="Id" description="Id of the opportunity" assignTo="{!oppId}" />

    <table border="2" cellspacing="5">
        <tr>
            <td>Action</td>
            <td>Activity Date</td>
            <td>Activity Subject</td>
            <td>Activity Status</td>
        </tr>
        <apex:repeat value="{!oppty}" var="opp"> 
        <apex:repeat value="{!opp.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>
         </apex:repeat>
    </table>
</apex:component>

and email template:
<messaging:emailTemplate subject="List of opportunity" recipientType="User" relatedToType="opportunity">
    <messaging:htmlEmailBody >
   <p>Hi {!relatedTo.owner.name},</p>
        <p>Please be informed {!relatedTo.name} is nearing the stage duration planned timeline of 14 days tomorrow.</p>

        <p>Kindly revert back with your plan for us to have the same updated in Salesforce.</p>

        <p>FYI, appended below is a snapshot of the last few activities related to this opportunity:</p>   
        <c:oppacct OpportunityId="{!relatedTo.Id}" /><br/><br/>
    <b>Regards,</b><br/>
    {!recipient.FirstName}
    </messaging:htmlEmailBody>
</messaging:emailTemplate>

Regards
Russell
 
This was selected as the best answer