0

I have a SOOQL query that retreives details about files uploaded, that works great.

Select id, ContentDocument.Title, ContentDocumentId, ContentDocument.LatestPublishedVersionId, ContentDocument.FileType from ContentDocumentLink where LinkedEntityId IN ('a2S0i0000001OQAEA2') 



Within the DocumentVersion object, there we've added a custom attribute that categorizes the file. But when i try to query it, it doesn't like the query.

Select id, ContentDocument.Title, ContentDocumentId, ContentDocument.LatestPublishedVersionId, ContentDocument.FileType, ContentDocument.ContentSize, ContentVersions.FileCategory__c  from ContentDocumentLink where LinkedEntityId IN ('a2S0i0000001OQAEA2') 

The error is

Didn't understand relationship 'ContentVersions' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

So i tried with __r but no luck. Is there a better way of doing this?

0

Your custom field is on ContentVersion and there is no direct relationship between ContentDocumentLink and ContentVersion. To indirectly link them, you will have to use their common field which is ContentDocumentId.

So if you only have the record id to while the file is linked (LinkedEntityId), then first query from ContentDocumentLink and store the ContentDocumentId in a collection, for example in a set called "docIdSet":

Set<Id> docIdSet = new Set<Id>();
for(ContentDocumentLink doc : [Select ContentDocumentId, 
                     ContentDocument.Title, ContentDocument.LatestPublishedVersionId
                   from ContentDocumentLink where LinkedEntityId IN ('a2S0i0000001OQAEA2')]){
    docIdSet.add(doc.ContentDocumentId);
}

Then query on ContentVersion to retrieve the custom field value and filter by ContentDocumentId:

select id, ContentDocument.Title, ContentDocumentId, ContentDocument.LatestPublishedVersionId,
    ContentDocument.FileType, ContentDocument.ContentSize, FileCategory__c 
        from ContentVersion 
        where contentdocumentid  IN :docIdSet

Note that the second query may bring more than 1 record if there are multiple versions of the file.

Hope this helps and answers your question

0

Addition to @Shamina clarification, If you want all details in one SOQL then you can use below query to fetch ContentDocument, ContentDocumentLink and contentversion data for one document record.

Select id, Title, LatestPublishedVersionId, FileType, ContentSize, 
(select id, ContentDocumentId from ContentDocumentLinks), 
(select id from contentversions) from ContentDocument

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.