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
Jordan@BracketLabsJordan@BracketLabs 

Chatter Subquery doesn't return related comments

I have a query where I'm trying to return the chatter posts and comments that pertain to a specific set of sObject's that were previously queried:

List<sObject__c> items = [SELECT Id FROM sObject__c ...];

Map<Id,sObject__Feed> projectTaskFeeds = new Map<Id,Project_Task__Feed>([SELECT Id, Type, CreatedById, CreatedBy.FirstName, CreatedBy.LastName, ParentId, Parent.Name, Body, Title, LinkUrl, ContentData, ContentFileName, (SELECT Id, CommentBody, CreatedDate, CreatedBy.FirstName, CreatedBy.LastName FROM FeedComments)
 FROM sObject__Feed WHERE ParentId IN :items LIMIT 50000]);

 But the resulting map only contains the sObject__c feed items, and not the related FeedComments.

Map<Id,sObject__Feed> projectTaskFeeds = new Map<Id,Project_Task__Feed>([SELECT Id, Type, CreatedById, CreatedBy.FirstName, CreatedBy.LastName, ParentId, Parent.Name, Body, Title, LinkUrl, ContentData, ContentFileName, (SELECT Id, CommentBody, CreatedDate, CreatedBy.FirstName, CreatedBy.LastName FROM FeedComments)
 FROM sObject__Feed LIMIT 50000]);

 

 

If I don't use the limiting 'WHERE' Clause however, I get the comments as well (but I got all items that are in the sObject__Feed table, which could easily be more than 50000 results in a large org.

 

Am I doing something wrong?

Best Answer chosen by Admin (Salesforce Developers) 
Jia HuJia Hu

Use the AccountFeed for example, just create some feed and comment on some Account,...

 

try the code below:

 

 

Map<Id, AccountFeed> mapaccfeed = new Map<Id, AccountFeed>([Select Id, Body, (Select Id, CommentBody, CreatedDate From FeedComments) From AccountFeed Where CommentCount > 0 Order by CreatedDate Desc Limit 10]);
System.debug(' ----------- ' + mapaccfeed);
Set<Id> idset = mapaccfeed.keyset();
for(Id aid : idset) {
List<FeedComment> fclist = (mapaccfeed.get(aid)).FeedComments;
for(FeedComment fc : fclist) {
System.debug(' ------------ ' + fc.CommentBody + ' ' + fc.CreatedDate );
}
}

 

You will see your Comment input now,...

Use CommentCount > 0 to query the Feed with Comment for demo,...

All Answers

Leon MorockiLeon Morocki

Did you try querying FeedItem table instead of sObject__Feed?

Jordan@BracketLabsJordan@BracketLabs

Actually, just reopened my debugger window and it started working...

 

Don't know if it works consistently yet but I think it was something in the debugger window!

Jia HuJia Hu

Use the AccountFeed for example, just create some feed and comment on some Account,...

 

try the code below:

 

 

Map<Id, AccountFeed> mapaccfeed = new Map<Id, AccountFeed>([Select Id, Body, (Select Id, CommentBody, CreatedDate From FeedComments) From AccountFeed Where CommentCount > 0 Order by CreatedDate Desc Limit 10]);
System.debug(' ----------- ' + mapaccfeed);
Set<Id> idset = mapaccfeed.keyset();
for(Id aid : idset) {
List<FeedComment> fclist = (mapaccfeed.get(aid)).FeedComments;
for(FeedComment fc : fclist) {
System.debug(' ------------ ' + fc.CommentBody + ' ' + fc.CreatedDate );
}
}

 

You will see your Comment input now,...

Use CommentCount > 0 to query the Feed with Comment for demo,...

This was selected as the best answer
Jordan@BracketLabsJordan@BracketLabs

Ultimately I found that I was recieving an error in the debugging window. When I ran the chatter reciepe code, it ran normally in another org.

Jia HuJia Hu
What is the error message?
Jordan@BracketLabsJordan@BracketLabs

There was no error message. It simply did not show the results in the debugging log.

Jia HuJia Hu
Make sure you have data to display, Object Feed with comment,...