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
MellowYellowMellowYellow 

Visualforce Report containing Case Comments

I am trying to create a report that contains CAse, Account and Opportunity data.  This works fine, but I am not able to include the most recent Case Comment.  The query returns the Case Commtnt, but I can't figure out how to access it from VF.  Here is my code so far;
public class retrieveCaseComments{
public List<Case> getCaseInfo() {
        return [Select CaseNumber,Owner.FirstName,Status,Subject, (Select CommentBody From CaseComments ORDER BY CreatedDate DESC LIMIT 1),  Opportunity_Name__r.StageName, Opportunity_Name__r.ExpectedRevenue,Opportunity_Name__r.CloseDate, Account.Name From Case Where Case.status != 'closed' LIMIT 50];
   // limit 50 for debug
 }  
}



<apex:page controller="retrieveCaseComments" >
    <apex:pageBlock title="Case Report" >
        <apex:pageBlockTable value="{!CaseInfo}" var="v" >     
            <apex:column value="{!v.CaseNumber}" headervalue="Case"/>
            <apex:column value="{!v.Owner.FirstName}" headerValue="Case Owner"/>
              <apex:column value="{!v.Subject}" headervalue="Subject" />      
            <apex:column value="{!v.Status}" headervalue="Status" />
<!-- need to show most recent Case Comment here -->
            <apex:column value="{!v.Opportunity_Name__r.StageName}" headervalue="Opportunity Stage" />
            <apex:column value="{!v.Opportunity_Name__r.ExpectedRevenue}" headervalue="Expected Revenue"/>
             Opportunity_Name__r.ExpectedRevenue
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Best Answer chosen by MellowYellow
MellowYellowMellowYellow
Finally figured it out.  I needed to use this to display the Case Comments;

         <apex:column >
        <apex:facet name="header">Case Comment</apex:facet>
        <apex:outputText rendered="{!IF(v.CaseComments.size > 0, TRUE, FALSE)}">
         <apex:outputText > {!v.caseComments[0].CommentBody}  </apex:outputText>
          </apex:outputText>
         </apex:column>    

All Answers

KevinPKevinP
<apex:page controller="retrieveCaseComments" >
    <apex:pageBlock title="Case Report" >
        <apex:pageBlockTable value="{!CaseInfo}" var="v" >     
            <apex:column value="{!v.CaseNumber}" headervalue="Case"/>
            <apex:column value="{!v.Owner.FirstName}" headerValue="Case Owner"/>
              <apex:column value="{!v.Subject}" headervalue="Subject" />      
            <apex:column value="{!v.Status}" headervalue="Status" />
            <apex:column value="{!v.caseComments[0].commentBody}" headervalue="Status" />
            <apex:column value="{!v.Opportunity_Name__r.StageName}" headervalue="Opportunity Stage" />
            <apex:column value="{!v.Opportunity_Name__r.ExpectedRevenue}" headervalue="Expected Revenue"/>
             Opportunity_Name__r.ExpectedRevenue
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Specifically, when you've a sub-query in your query, you'll need to provide an index of the array you wish to access.

using v.caseComments[0] should give you the first caseComment (and only case comment since you've a Limit 1) object that you can then display the commentBody from
MellowYellowMellowYellow
Thanks!  That allows the Visualforce to compile. Now I'm getting an error when viewing the page:  'Subscript is invalid because list is empty'
I assume that this is because some of the Cases do not have any Comments.  Any ideas on how to handle this condition?  I was trying to use an ISNULL formula in the Visualforce, but couldn't get this to work.

 
MellowYellowMellowYellow
Finally figured it out.  I needed to use this to display the Case Comments;

         <apex:column >
        <apex:facet name="header">Case Comment</apex:facet>
        <apex:outputText rendered="{!IF(v.CaseComments.size > 0, TRUE, FALSE)}">
         <apex:outputText > {!v.caseComments[0].CommentBody}  </apex:outputText>
          </apex:outputText>
         </apex:column>    
This was selected as the best answer