• RajeshShah.ax1315
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies

Recently I was working on a requirement which required overriding the Case detail page with a visualforce page. Problem crept when the related list comonent didnt worked for Case Comments and History. So I decided to write my own code to display the related lists.

I was finally able to write my own code to display the related list. It implements all the function that standard related list of Case Comments provide. In order to make it reusable, I made it as a component so that in other pages I just need to pass the Case Id and I will get the complete list of Case Comments.

 Heres the code finally :

 1. Component Code

 

<apex:component controller="CaseCommentsComponentController" allowDML="true">
<!-- Attribute Definition -->
<apex:attribute name="CaseId" description="Salesforce Id of the Case whose Case Comments needs to be rendered" type="Id" required="true" assignTo="{!caseId}" />

<!-- Component Body -->
<apex:componentBody >
<apex:form >
<apex:pageBlock title="Case Comments" >
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!NewComment}" value="New"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!Comments}" var="comment">
<apex:column headerValue="Action">
<apex:outputLink value="/{!comment.cComment.Id}/e?parent_id={!caseId}&retURL=/{!caseId}">Edit</apex:outputLink>&nbsp | &nbsp
<apex:commandLink action="{!deleteComment}" value="Del">
<apex:param name="CommentId_d" value="{!comment.cComment.Id}"/>
</apex:commandLink>&nbsp | &nbsp
<apex:commandLink action="{!makePublicPrivate}" value="{!comment.PublicPrivateAction}">
<apex:param name="CommentId_p" value="{!comment.cComment.Id}" />
</apex:commandLink>
</apex:column>
<apex:column headerValue="Public" value="{!comment.cComment.IsPublished}" />
<apex:column headerValue="Comments">
<apex:outputText escape="false" value="{!comment.commentText}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:componentBody>
</apex:component>

 

 2. Apex Code

public with sharing class CaseCommentsComponentController {

public Id caseId {get; set;}
public cComments[] comments{
get{
List<cComments> comments = new List<cComments>();
for(CaseComment comment : [Select LastModifiedDate, LastModifiedBy.Id, LastModifiedBy.Name, IsPublished, CreatedDate, CreatedBy.Id, CreatedBy.Name, CommentBody From CaseComment c where ParentId = :caseId order by c.LastModifiedDate desc])
{
cComments tempcComment = new cComments();
tempcComment.cComment = comment;

// Build String to display.
tempcComment.commentText = '<b>Created By: <a href=\'/' + comment.CreatedBy.Id + '\'>' + comment.CreatedBy.Name + '</a> (' + comment.CreatedDate.format() + ') | ';
tempcComment.commentText += 'Last Modified By: <a href=\'/' + comment.LastModifiedBy.Id + '\'>' + comment.LastModifiedBy.Name + '</a> (' + comment.LastModifiedDate.format() + ')</b><br>';
tempcComment.commentText += comment.CommentBody;

if(comment.IsPublished)
tempcComment.PublicPrivateAction = 'Make Private';
else
tempcComment.PublicPrivateAction = 'Make Public';
//Add to list
comments.add(tempcComment);
}
return comments;
}

set;
}

public PageReference NewComment()
{
PageReference pr = new PageReference('/00a/e?parent_id='+ caseId + '&retURL=%2F' + caseId);
pr.setRedirect(true);
return pr;
}

public PageReference deleteComment()
{
Id commentId = ApexPages.currentPage().getParameters().get('CommentId_d');

for(cComments Comment : comments)
{
if(Comment.cComment.Id == commentId)
{
delete Comment.cComment;
break;
}
}

PageReference pg = new PageReference('/' + caseId);
pg.setRedirect(true);
return pg;
}

public PageReference makePublicPrivate()
{
Id commentId = ApexPages.currentPage().getParameters().get('CommentId_p');
for(cComments Comment : comments)
{
if(Comment.cComment.Id == commentId)
{
Comment.cComment.IsPublished = !Comment.cComment.IsPublished;
if(Comment.cComment.IsPublished)
Comment.PublicPrivateAction = 'Make Private';
else
Comment.PublicPrivateAction = 'Make Public';

update Comment.cComment;
break;
}
}
PageReference pg = new PageReference('/' + caseId);
pg.setRedirect(true);
return pg;
}

public class cComments {

public CaseComment cComment {get; set;}
public String commentText {get; set;}
public String PublicPrivateAction {get; set;}
}
}

 Hope it is helpful to you.

 Let me know your views on the code above or if you have any questions

Message Edited by Rajesh Shah on 10-16-2009 08:03 PM