You need to sign in to do that
Don't have an account?

Case Comments Related List using Visualforce
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>  |  
<apex:commandLink action="{!deleteComment}" value="Del">
<apex:param name="CommentId_d" value="{!comment.cComment.Id}"/>
</apex:commandLink>  |  
<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
Rajesh.
This great you put this component together.
Couple questions and issues I have with it:
I embeded your component as follows:
<apex:page standardController="Case"> <c:CaseCommentRelatedList CaseId="{!Case.Id}"/> </apex:page>
Then I added this visual force page to my case standard Page Layout as a separate section. The problem is that it displays only several comments and if I do have more comments and longer in length then it will not work for me.
Any ideas...perhaps I don't embed your component properly?
Thank you
You mean that if there are 10-15 comments but you want to display only the last 5 or something like that?
Well pageBlockTable has a attribute called rows. You can set that to the number you want.
No actually the way around. If I have 2 rows of comments they display fine, however if I have more comments they don't display on the page.
is there a property for a scroll bar I can set for example if the content exceeds the size of the page I can set the scroll bar.
I have a question about your code.
The following line:
What class type is cComments I don't understand? Is it a dynamic class that you create on the fly to place you temp variables?
I tried to re-create your cComments class type in a different code and place some dynamic variabl, but didn't work.
Can you please explain and correct me where I am wrong.
Thanks in advance.
Rajesh, Thanks for this component!
For anyone needing a Unit Test in order to push into production, I have created the following unit test. Feel free to use this with Rajesh's original posted component code.
Mike Katulka
www.itedgecrm.com
IT Edge CRM, Inc.
Hi, this code is brilliant, i am using it in a service cloud setup and have replaced the interaction panel with this.
I just have one question, is there a way of modifying the "new" button so that when it's clicked it opens in a new window? or the main page in the the service cloud?
You can use CommandLink and targer = "_blank" to open a new window. Later, you can also style the link as button.
The "_blank" seems to work fine as a commandlink but is there a way of doing this as a commandbutton?
am i able to add it in the page ref in the class? in this bit?
As far as I know, it is not possible. You can do one more thing. From commandButton, call apex function with pagereference null. Use oncomplete event of commandButton to call JS function and open the new window from there.
this is a great code. thanks for this!
one question though:
we're trying to put this into a Force.com public site.
The public access settings (profile) doesnt allow for Case Editing. Only Read and Create.
Will creating a new comment from a Force.com site work?
Hi
I'm trying to use this test class and it is not passing. The apex class works and I can use it on a page in my sandbox but if anyone could tell me why the test isnt completeing that would be great.
Apex class
Apex Test Class
Component
Page
If anyone could help this test class pass it would be great.
Cheers,
Ritchie
Hi All,
Any ideas on this?
Kind Regards,
Ritchie
Hi All,
Nevermide I have worked it out. The problem was with the create case section, it had a record type that didnt exsist and a status that also didnt exsist.
I'm try to use this visualforce component at my Saleforce site VF page it's not working, Can anybody tell me how can I use this component at my saleforce site VF page and other case related lists.
Thanks in advanced
I saw all above comments.
I have created VF page in which i am able to delete casecomment commentbody, but i want to know how can i edit any casecomment and save on same page.