11

Trying to perform a SOQL query to retrieve related FeedItems and FeedComments (on the related FeedItems) to my sObject and I retrieved a warning that multi-level queries aren't allowed.

Can anyone think of an optimization that will reduce the risk of overflowing script statements and allow me to retrieve related FeedItems and the FeedComments related to those items

List<myObject> sObjectsWithComments = [SELECT Id (SELECT Id,Body,ParentId, (SELECT 
                                       Id, CommentBody, FeedItemId FROM FeedComments) FROM
                                       Feeds) FROM myObject];

If this isn't achievable it's still possible to manually selected the FeedComment and create a map, and do a manually association, but I really think this is something the database should be responsible for.

Update

I guess I should also mention some of the difficult requirements:

  1. List item: In any given instance the page I'm working with may have over 100 object records
  2. I need count the number of FeedItems and FeedComments that have been created by another user since a LastVisit__c field on the parent object. Hence the issue with looping

So I have a goal of something more akin too (psuedo-code):

List<myObject> sObjectsWithComments = [SELECT Id,LastVisit__c (SELECT Id,Body,ParentId, (SELECT 
       Id, CommentBody, FeedItemId, CreatedDate FROM FeedComments WHERE CreatedDate > Parent.LastVisit__c) FROM
       Feeds WHERE CreatedDate > Parent.LastVisit__c) FROM myObject];
11

Separate the feed from the object to avoid a nested query.

List<FeedItem> feedWithComments = [SELECT Id,Body,ParentId, (SELECT 
                                   Id, CommentBody, FeedItemId FROM FeedComments) FROM
                                   FeedItem WHERE ParentId IN (SELECT Id FROM
                                   myObject WHERE Field = 'value')];
  • updated some of my requirements, I forgot to mention I'm already doing this, but I need to reduce the results based on a field in each of the parent sobject so I have to loop and match each record with it's parent manually because I can't limit each set of related records by a parent objects field value. – jordan.baucke Jul 31 '12 at 20:52
  • You can do that by executing a correlated subquery in your where clause: List<FeedItem> feedWithComments = [SELECT Id,Body,ParentId, (SELECT Id, CommentBody, FeedItemId FROM FeedComments) FROM FeedItem WHERE ParentId IN (SELECT Id FROM <Object> WHERE <Field> = <value>)]; – E.J. Wilburn Aug 1 '12 at 0:59
  • @jordan.baucke Did the correlated subquery answer your question? Is this question still open? – metadaddy Aug 12 '12 at 17:31
  • I'll give it a shot... – jordan.baucke Aug 12 '12 at 18:03

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.