Query on ContentVersion returns 0 results immediately after querying ContentDocumentLink.
Last updated 2018-05-05 ·Reference W-4794583 ·Reported By 2 users
Summary
Querying ContentDocumentLink then ContentVersion (for a large amount of files), the ContentVersion query fails and returns 0. Similarly, if you do some other query between ContentDocumentLink and ContentVersion, the ContentVersion query succeeds
Repro
Running the following snippet of code in execute anonymous window:
Map <String , Case> caseMap;
caseMap = new Map<String, Case>([Select ID, CaseNumber, CreatedDate from Case where CreatedDate >= 2017-11-1T00:00:00Z AND CreatedDate <= 2017-12-12T00:00:00Z Order By CreatedDate asc ] );
System.debug('Case Size: ' + caseMap.keySet().size());
if (caseMap != null && caseMap.size() > 0){
List <String> entityIDs = new List <String> ();
for (String s : caseMap.keySet()) {
entityIDs.add(s);
}
if (entityIDs != null && entityIDs.size() > 0) {
List <ContentDocumentLink> dList = [Select ID, ContentDocumentId, LinkedEntityId from ContentDocumentLink where LinkedEntityId in : entityIDs];
if (dList != null && dList.size() > 0) {
List <ID> docIDs = new List <ID> ();
for (ContentDocumentLink cdl : dList) {
docIDs.add(cdl.ContentDocumentId);
}
List <ContentVersion> cvList = [Select ID, ContentDocumentId, Title, ContentSize, FileType, FIleExtension from ContentVersion where ContentDocumentId in : docIDs And FileExtension in ('zip', 'gzip', 'gz', 'tar', 'rar', 'bz2', '7z', 'tgz', 'zipx') ];
Map <String, ContentVersion> docID_contentIdMap = new Map <String, ContentVersion> ();
if (cvList != null && cvList.size() > 0) {
for (ContentVersion cv : cvList ) {
docID_contentIdMap.put(cv.ContentDocumentID, cv );
}
List <ContentDocumentLink> contextCDLList = [Select ID, ContentDocumentID, LinkedEntityID from ContentDocumentLink Where LinkedEntityID in : entityIDs And ContentDocumentID in :docID_contentIdMap.keySet()];
if (contextCDLList != null && contextCDLList.size() > 0) {
System.debug('Document Link Count: ' + contextCDLList.size());
for (ContentDocumentLink cd : contextCDLList) {
ContentVersion contextVersion = docID_contentIdMap.get(cd.ContentDocumentID);
String caseId = cd.LinkedEntityID;
}
}
}
}
}
}
The expected flow seen is:
[execute_anon_apex]
[query] Select ContentDocumentId From ContentDocumentLink
[query] Select ContentDocumentId From ContentVersion Where
The actual flow seen is:
[execute_anon_apex]
[query] Select ContentDocumentId From ContentDocumentLink ...
[queryMore] Select ContentDocumentId From ContentDocumentLink ...
[query] Select ContentDocumentId From ContentVersion Where ...
Notice There is an additional [queryMore]
It has been investigated to find out that the issue is with ContentDocument link & it does not clear temporary data used in making the query.
Workaround
1) Add LIMIT (greater than the expected number of records) to the query on ContentDocumentLink
2) Merge the queries Eg:
SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = '005*' AND ContentDocument.LatestPublishedVersion.FileExtension in ('zip', 'gzip', 'gz', 'tar', 'rar', 'bz2', '7z', 'tgz', 'zipx')
Reported By (2)
Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.