2

I want to get the associated CaseComment's CommentBody for a list of cases.

String queryFields = 'Id, Subject, CaseComment.CommentBody';
 List<Case> cases = Database.query('select '+ queryFields +' from Case');
 for(Case c: cases){
       System.debug(c.CaseComment.CommentBody);
 }

but this doesn't work as there is no reference in Case object for the CaseComment. CaseComment has the field parentId which stores id of associated Case. How can I get CaseComment. I don't want to query the CaseComment object for each case in a loop.

2 Answers 2

6

You must traverse the child relationship CaseComment, for example:

List<Case> cases = [Select (Select Id, ParentId, IsPublished, CommentBody, CreatedById, CreatedDate, SystemModstamp, LastModifiedDate, LastModifiedById, IsDeleted From CaseComments) From Case ];

for(Case c : cases)
{
    for(CaseComment comm : c.CaseComments)
    {
        System.debug(comm.CommentBody);
    }
}

This is because there are many Case Comments to one Case, i.e. a one to many relationship between case and comment as per the master/detail or lookup relationship.

You should consider using Eclipse schema viewer so that you can find these relationships out yourself, its a simple point/click interface so you can traverse them graphically.

4
  • Thanks for you answer. I also need EmailMessage associated with case. In EmailMessage object (salesforce.com/us/developer/docs/api/Content/…) I see a ParentId field that says 'id of parent case', but in eclipse Schema viewer I don't see any child relationship for email messages
    – vishesh
    May 10, 2014 at 9:48
  • 1
    Same deal, [select id, (select id from emailmessages) from case] but I think you have to enable 'Email To Case' in org to see the object May 10, 2014 at 10:00
  • @Phil Hawthorn Correct me if i'm working it's a bad practice to keep for loop within a for loop?
    – Pavan tej
    Jun 16, 2021 at 20:00
  • @Pavantej I don't know really, I suppose it would depend on what you're doing in that for loop. As an illustrative example, I think its fine but in production code perhaps not. Jun 17, 2021 at 14:54
0

I'm posting this answer because sometimes nested loops will reach out to CPU limit's enforced by the governor limits. So, try to avoid as much as possible.

Yes in this case you need to traverse the child relationship Case Comment, because one case have multiple case comments

Map<String, Schema.RecordTypeInfo> allcaserecordTypes = Schema.SObjectType.Case.getRecordTypeInfosByName();
List<CaseComment> AllCasecmts= new List<CaseComment>();
Map<Id,List<Casecomment>> nestedcomments = new Map<id,List<Casecomment>>();
For(Case css:[Select id,(Select id,parentid,IsPublished from CaseComments where IsPublished=false ) from case where 
        recordtypeid=:allcaserecordTypes.get('Fleet').recordtypeid])
{
    // Based on your convenience add all the case comments in to a list or a map
    AllCasecmts.addall(css.casecomments);
    nestedcomments.put(css.id,css.casecomments);
}

I hope this solution will be helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.