You need to sign in to do that
Don't have an account?
MellowYellow
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>
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>
<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
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
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.
<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>