function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Will LylesWill Lyles 

How can I get a list of case comments using a custom controller

I am trying to use a custom controller to get back case comments for a particular case (case id will be in URL).  I need to do this so I can sort columns (very annoying that Salesforce doesn't allow this with dataTables).  Right now I'm just trying to get some data back from my controller and failing miserably.

How can I do this?

So far this doesn't work....
****My test VF page****
<apex:page standardController="Case" extensions="TestController"> 
    <apex:form >   
        <apex:pageBlock title="{!Case.CaseNumber}"> 
			TEST:{!caseComm[0].CommentBody}
        </apex:pageBlock>   
    </apex:form>  
</apex:page>
***My test controller***
public class TestController {
    public List<Case> caseComm {get;set;}
    
    public TestController(ApexPages.StandardController controller) {
		
    }
    
    public List<Case> caseComm() {   
		List<Case> comments = [Select (Select Id, 
                            ParentId,  
                            IsPublished, 
                            CommentBody, 
                            CreatedById, 
                            CreatedDate,                  
                            LastModifiedDate, 
                            LastModifiedById, 
                            From CaseComments) From Case ];
        return comments;
    }
    
    
}


 
Best Answer chosen by Will Lyles
Rajesh3699Rajesh3699
Hi, 

I think you need to add the 

Public list<CaseComment> getCaseCommentList()
{
return caseCommentList;
}


Refer the below code, 

 
<apex:page StandardController="Case" extensions="caseCommentCtrl">
    <apex:outputText value="{!case.CaseNumber}" />
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!caseCommentList}" var="com">
                <apex:column value="{!com.ParentId}" />
                <apex:column value="{!com.CommentBody}" />
            </apex:pageBlockTable>
        </apex:pageBlock>       
    </apex:form>
</apex:page>
 
Public Class caseCommentCtrl{


Public list<CaseComment> caseCommentList{get;set;}

Public list<CaseComment> getCaseCommentList(){
    return caseCommentList;
}
    
Public caseCommentCtrl(ApexPages.StandardController ctrl){
        Case c = [select Id, CaseNumber,(select ParentId,CommentBody from CaseComments) from Case where Id =: ApexPages.CurrentPage().getParameters().get('Id')];
        caseCommentList = new List<CaseComment>();
        for(CaseComment cs : c.CaseComments){
            caseCommentList.add(cs);
        }
    }
}

User-added image

Thank You,
Rajesh Adiga P.

All Answers

Rajesh3699Rajesh3699
Hi, 

I think you need to add the 

Public list<CaseComment> getCaseCommentList()
{
return caseCommentList;
}


Refer the below code, 

 
<apex:page StandardController="Case" extensions="caseCommentCtrl">
    <apex:outputText value="{!case.CaseNumber}" />
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!caseCommentList}" var="com">
                <apex:column value="{!com.ParentId}" />
                <apex:column value="{!com.CommentBody}" />
            </apex:pageBlockTable>
        </apex:pageBlock>       
    </apex:form>
</apex:page>
 
Public Class caseCommentCtrl{


Public list<CaseComment> caseCommentList{get;set;}

Public list<CaseComment> getCaseCommentList(){
    return caseCommentList;
}
    
Public caseCommentCtrl(ApexPages.StandardController ctrl){
        Case c = [select Id, CaseNumber,(select ParentId,CommentBody from CaseComments) from Case where Id =: ApexPages.CurrentPage().getParameters().get('Id')];
        caseCommentList = new List<CaseComment>();
        for(CaseComment cs : c.CaseComments){
            caseCommentList.add(cs);
        }
    }
}

User-added image

Thank You,
Rajesh Adiga P.
This was selected as the best answer
PINKY REGHUPINKY REGHU
Hi,
Try the code given below,
vf page:
<apex:page standardController="Case" extensions="case_comment"> 
    <apex:form >   
        <apex:pageBlock title="{!Case.caseNumber}">
            <apex:pageBlockTable value="{!CaseCommentsSortedASC}" var="c" >
                <apex:column value="{!c.commentbody}"></apex:column>
           </apex:pageBlockTable>
        </apex:pageBlock>   
    </apex:form>  
</apex:page>

controller:
public class case_comment {
    public case caseComm;
    public List<CaseComment> CaseCommentsSortedASC{get;set;}
    public case_comment(ApexPages.StandardController controller) {
      this.caseComm=(case)controller.getRecord();
        system.debug('caseComm'+caseComm);
        caseComm1(caseComm);
    }
    public void caseComm1(case caseRec) {
        CaseCommentsSortedASC=new List<CaseComment>();
     for(Case c : [SELECT (SELECT CommentBody, ParentId FROM CaseComments WHERE ParentId = :caseRec.Id) FROM Case]) {
            for (CaseComment cc : c.CaseComments) {
                 CaseCommentsSortedASC.add(cc);
            }
         }
   }
}

Thanks,
Pinky
Will LylesWill Lyles
Rajesh/Pinky - Thanks so much for responding to my post.  I ended up using Rajesh's code with a small addition to get the sorting to work.
 
Case c = [select Id, CaseNumber,(select ParentId,CommentBody,CreatedDate from CaseComments ORDER BY CreatedDate DESC) from Case where Id =: ApexPages.CurrentPage().getParameters().get('Id') ];

This works perfectly for what I am trying to do.

Thanks again!
Rajesh3699Rajesh3699
Most welcome :-) 

Thank You,
Rajesh Adiga P.