0

I get this error when I run this query. The inner and outer selects should not be on the same object.

select Id from contentFolderItem where ParentContentFolderId in
(select Id from contentfolderitem where parentContentFolderId = 
'abcdefggggg')

If I split the queries I have to use a for loop which sometimes results in "101 Too many soql queries" error.

I understand why I get both the errors, but I don't know how to fix the problem.

I need to run this soql from apex.

2
  • 1
    What are you trying to achieve by adding an inner query on the same object. Can you not add parentContentFolderId = 'abcdefggggg' in the main query.
    – RedDevil
    Jul 1 '17 at 9:28
  • The content structure seems a bit odd since an item can be a folder but the parent is a contentfolder. Seems like some weird polymorphic stuff going on there but I don't have an org with content populated to test right now. I think this query might help you out: select Id from contentFolderItem where parentcontentfolderid in (select id from contentfolder where parentcontentfolderid = 'abcd...')
    – drakored
    Jul 1 '17 at 10:53
3

What you're trying to do is called a semi-join (or an anti-join if you use NOT IN). These are useful for gathering table A if it contains or does not contain some relational object (e.g. find all accounts with a contact named Bob).

SELECT Id,Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact WHERE FirstName = 'Bob')

A sub-query is slightly different. It's a left inner join that grabs all of table A, and any of the table B records (respecting the criteria you give).

SELECT Id,Name,(SELECT Id,Name FROM Contacts WHERE FirstName = 'Bob') FROM Account

This will give me all accounts whether they have a contact named Bob or not. You can read up more on semi and anti-joins in the documentation here: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_comparisonoperators.htm

Specific to your issue though, the object you're working with appears to be somewhat abstract. It appears as though a ContentFolderItem can be either a ContentDocument or a ContentFolder. You'll notice the ContentFolderItem has a flag called IsFolder. You can read more on the object here: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentfolderitem.htm

In this case you can just query the ContentFolderItem object as a semi-join to the ContentFolder object:

SELECT Id FROM ContentFolderItem WHERE ParentContentFolderId IN (SELECT Id FROM ContentFolder where ParentContentFolderId = 'abcd...')

This will give you all ContentFolderItems (documents and folders) inside the folder whose parent folder has the Id 'abcd...'. Normally you could just traverse the relationships upward like this:

SELECT Id FROM ContentFolderItem WHERE ParentContentFolder.ParentContentFolderId = 'abcd...'

however... this is a special object that requires filtering on Id. You can't run very broad queries on these tables.

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.