6

If I want to find how many files are related to a given object (ex. Account), I could do the following two queries (assuming you don't hit a 50,000 row limit - this is just an example).

//get accounts
Map<Id,Account> accountsById = [SELECT Id FROM Account];

//Find all ContentDocuments related to all those accounts
List<ContentDocumentLink> contentDocumentLinks = [SELECT Id 
FROM 
ContentDocumentLink 
WHERE LinkedEntityId IN :accountsById.keySet()];

It seems I do not get the full picture depending on the size of accountsById.keySet().

I replicated this by adding a CreatedDate filter to my accounts query (to lower the number of returned record incrementally) and noticing the size of the returned contentDocumentLinks actually increased as I lessened the scope.

Map<Id,Account> accountsById = [SELECT Id 
FROM Account 
WHERE CreatedDate > 2020-09-01T00:00:00z]; //10,000 records returned

//Find all ContentDocuments related to all those accounts
List<ContentDocumentLink> contentDocumentLinks = [SELECT Id 
FROM ContentDocumentLink 
WHERE LinkedEntityId IN :accountsById.keySet()];

System.debug(contentDocumentLinks.size()) // 33

---------------------------------------------------------

Map<Id,Account> accountsById = [SELECT Id 
FROM Account 
WHERE CreatedDate > 2020-12-01T00:00:00z]; //5,000 records returned

//Find all ContentDocuments related to all those accounts
List<ContentDocumentLink> contentDocumentLinks = [SELECT Id 
FROM ContentDocumentLink 
WHERE LinkedEntityId IN :accountsById.keySet()];

System.debug(contentDocumentLinks.size()) // 40

Is there any rationale for why I would get more results in my ContentDocumentLink query when I'm removing records?

I saw no indication of some sort of limit for the IN operator in ContentDocumetLink. Only mention I see is a previous known issue (marked fixed in Summer '19) that seems to be a similar scenario.

Update: Playing around with query plan and getting the cost for various sized IN operators and saw some large spikes that I think correlate with certain Ids returning more files than others. Each Id in the in operator added to the number of records returned. Important to note my original query is dealing with a lot more numbers (5,000 + records) compared to the query plan test which I maxed out at 250.

enter image description here

4
1

Worked with support. It required them to enable an internal permission to overcome the issue which seems to be a limitation of 2,000 records to filter the LinkedEntityId by before you start seeing incorrect query results. Per the rep,

As per the information from R&D team and the KI the issue comes if there are more than 2000 records in the LinkedEntityId and this perm will increase the maximum record Ids to return in the query.

By enabling the perm this can be resolved which can be done by Salesforce Support.

Hence this is related to the bug on the KI and the resolution is to enable the internal perm.

It seems that the known issue in my question that was deemed "fixed" is actually only fixed with this permission they enable. Once there are more than 2,000 records in the LinkedEntityId - the results may be inaccurate without it.

In terms of how inaccurate, I was getting the following data before:

  • 5,000 records in LinkedEntityId, 40 records returned
  • 10,000 records in LinkedEntityId, 33 records returned

After the perm was enabled

  • 5,000 records in LinkedEntityId, 94 records returned
  • 10,000 records in LinkedEntityId, 220 records returned

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.