5

I have a visualforce email that show all case comments within a case. I'd like to sort the comments by created date (which is one of the columns). How can I achieve that?

    <messaging:emailTemplate recipientType="User"
    relatedToType="Case"
    subject="Case #{!relatedTo.CaseNumber} is Closed"
    replyTo="myemail" >

<messaging:htmlEmailBody >        
    <html>
        <body>
         <STYLE type="text/css">
               TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center } 
               TD  {font-size: 11px; font-face: verdana } 
               TABLE {border: solid #CCCCCC; border-width: 1}
               TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
                  <font face="arial" size="2">
        <p>case<b> {!relatedTo.CaseNumber}</b> was closed </p>
        <br/>Account: <b> {!relatedTo.Account.name}  </b>  
        <br/>Case Owner: {!relatedTo.owner.name}
        <br/>Below is a list of Case Comments related to this case

       <table border="0" >
                 <tr > 
                     <th>Comments</th><th>Created Date</th><th>Public</th> <th>Created By</th>
                  </tr>
    <apex:repeat var="opp" value="{!relatedTo.CaseComments}">
       <tr>
           <td>{!opp.CommentBody}</td>
           <td>{!opp.CreatedDate}</td>
           <td>{!opp.IsPublished}</td>
           <td>{!opp.CreatedBy.Name}</td>
             </tr>
    </apex:repeat>                 
       </table>
       <p />
 </font>      
        </body>
    </html>
</messaging:htmlEmailBody> 

<messaging:plainTextEmailBody >

Below is a list of Case Comments related to the Case: {!relatedTo.CaseNumber}

Account: {!relatedTo.Account.name}
Opportunity Owner: {!relatedTo.owner.name}


[ Comments ] - [ Created Date] - [ Public ]
-------------------------------------------------------------------------

<apex:repeat var="opp" value="{!relatedTo.CaseComments}">
[ {!opp.CommentBody} ] - [ {!opp.CreatedDate}  ] - [ {!opp.IsPublished} ]
</apex:repeat>

</messaging:plainTextEmailBody>       

</messaging:emailTemplate>

1 Answer 1

6

You could use a Visualforce Component to requery the CaseComments in the order you desire:

Comments.page

<apex:page standardController="Case">
  <apex:sectionHeader title="{!Case.Subject}" />
  <c:sortedCaseComments caseId="{!Case.Id}" />
</apex:page>

sortedCaseComments.component

<apex:component controller="SortedCaseCommentsController">
  <apex:attribute name="caseId" type="Id" assignTo="{!AttributeCaseId}" description="Case Id" />

  <apex:dataList value="{!CaseComments}" var="caseComment">
    {!caseComment.Id} - {!caseComment.CommentBody}
  </apex:dataList>
</apex:component>

SortedCaseCommentsController.cls

public class SortedCaseCommentsController {
  public Id     AttributeCaseId    {get; set;}

  public List<CaseComment> getCaseComments() {
    return [
      SELECT CommentBody
      FROM CaseComment
      WHERE ParentId = :this.AttributeCaseId
      ORDER BY CreatedDate DESC
    ];
  }

}

sorted case comments

2
  • herp, missed your component declaration and attribute off, also don't you want to sort your SOQL? Commented Mar 12, 2013 at 18:39
  • Thanks @user320! could you post the test coverage for this controller?
    – Itay B
    Commented Mar 14, 2013 at 9:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .