+ Start a Discussion
Daphne CalitzDaphne Calitz 

Display Case Emails and Case Comments together on VF Page

Hi all,

I need to be able to display all my case comments and case emails in an <apex:repeat> block and they should be ordered by the date they were sent. I'm rather new to salesforce development and I'm not 100% sure how to do this. 

I have tried the following:

My Controller:

public class CaseFeedItem
    {
        public string creatorSmallPhotoUrl {get; set;}
        public string createdByName {get; set;}
        public string textBody {get; set;}
        public dateTime createdDate {get; set;}        
    }
    
    public list<CaseFeedItem> myCaseFeedItems {get; set;}

PageReference loadcase(Id caseID) 
    {
        system.debug('loadcase: starting... ');

        if (caseID == null)
            return page.conf_error;  /// NEED TO DO SOMETHING HERE
        
        mycase = 
            [
                SELECT
                        Id,
                        CaseNumber,
                        Subject,
                        Owner.Name,
                        CreatedDate,
                        Status,
                        Description,
                        Auditor_First_Name__c,
                        Auditor_Last_Name__c,
                        Auditor_Email__c,
                        Auditor_Phone__c,
                        Client_Name__c,
                        Client_Year_End_Date__c,
                        Client_No_of_Requests__c,
                        Client_Date_Requests_were_Sent__c,
                        Client_Audit_Deadline_Date__c,
                        ClosedDate,
                		IsClosed
                    FROM 
                        Case
                    WHERE
                        Id = :caseID
            ];
        
        system.debug('viewcase: case: '+mycase.subject);
        
        mycasecomments = 
            [
                SELECT 
                    Id,
                    CommentBody,
                    CreatorName,
                    CreatedById,
                    CreatedDate,
                    CreatorFullPhotoUrl,
                    CreatorSmallPhotoUrl,
                    IsPublished,
                    CreatedBy.Name
                    
                FROM 
                    CaseComment
                WHERE 
                    ParentID = :caseID
                ORDER BY
                    CreatedDate DESC
            ];
 
        myCaseEmails = 
        [
            SELECT
            	Id,
            	FromAddress,
            	FromName,
            	TextBody,
            	MessageDate,
            	ToAddress,
            	Subject
            	
            FROM
            	EmailMessage
            WHERE
            	ParentID = : caseId
            ORDER BY
            	MessageDate DESC
        ];
        newcomment = new CaseComment(ParentId=caseID);
        createCaseFeed();
        return null;

    }

 public void createCaseFeed()
    {
        for(CaseComment comment : mycasecomments)
        {
            CaseFeedItem feedItem = new CaseFeedItem();
            feedItem.creatorSmallPhotoUrl = comment.CreatorSmallPhotoUrl;
            feedItem.createdByName = comment.CreatedBy.Name;
            feedItem.textBody = comment.CommentBody;
            feedItem.createdDate = comment.CreatedDate;
            myCaseFeedItems.add(feedItem);
        }
        
        for(EmailMessage email : myCaseEmails)
        {
            CaseFeedItem feedItem = new CaseFeedItem();
            feedItem.creatorSmallPhotoUrl = null;
            feedItem.createdByName = email.FromName;
            feedItem.textBody = email.TextBody;
            feedItem.createdDate = email.MessageDate;
            myCaseFeedItems.add(feedItem);
        }
    }
And on my VF page:
 
<apex:repeat value="{!CaseFeedItems}" var="feedItem">
                        
      <div class="col-md-2 col-sm-2 xs-marginbottom">
            <img src="{!feedItem.creatorSmallPhotoUrl}" width="60" height="60" alt=""/><br/>
             <apex:outputText value="{!feedItem.createdByName}" />
       </div>
       <div class="col-md-10 col-sm-10">
              <apex:outputText value="{!feedItem.textBody}" />
               <br/><br/>
               <small>Date/Time Sent:&nbsp; 
                  <apex:outputText value="{0,date,MM'/'dd'/'yyyy  HH:MM}">
                        <apex:param value="{!feedItem.createdDate}" /> 
                  </apex:outputText>
               </small> 
       </div>
       <div class="col-md-12 col-sm-12">
           <hr/>
        </div>
</apex:repeat>


At the moment I get:

Error: Error occurred while loading a Visualforce page. on /confirmation/conf_caseView
Attempt to de-reference a null object 

And I would also like to find out how I would sort the "myCaseFeedItems" list by date

Daphne CalitzDaphne Calitz
I managed to sort out my error by initializing the list in the controller constructor and checking that there is any results before looping through them. But I still have no idea how to sort the list by date. If anyone could help me I would really appreciate it.
Rajiv B 3Rajiv B 3
Hi Daphnee, 

Can you please share the final code which is working for your requirment. It will help for others ... 
Bullfrog84Bullfrog84
What I've done if the asc and desc in the SOQL doesn't work is apply jquery tablesorter. http://tablesorter.com/docs/ and you can find quite a few examples.