function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Esha Sharma 4Esha Sharma 4 

Querying content version returns no rows

        I am trying to save a content version document.Below is what I am doing 

      qr = partner_connection.query("SELECT VersionData from ContentVersion WHERE ContentDocumentID = '" + id + "'");
             
        System.out.println(qr.getSize() > 0);  //returns false.
Douglas C. AyersDouglas C. Ayers
Two things that may make that query return zero records:
1. The ID variable does not hold a ContentDocument ID (069 prefix) (something else like a ContentVersion ID (069 prefix))
2. The ContentVersion record is not explicitly shared to the context user via ContentDocumentLink record

To solve for (1), confirm the ID value.

To solve for (2), see below:

ContentVersion and ContentDocument have unique sharing restrictions as compared to other objects in Salesforce. Querying ContentVersion directly will only return records explicitly shared to the user via ContentDocumentLink object.

Even if a user is an admin with View All / Modify All permission, the only way to query for files that are not explicitly shared to the user record is to query indirectly through ContentDocumentLink on records/groups/libraries that the file may have been shared with.

For example, if the file in your code example is not explicitly owned/shared to the context user but perhaps is shared to say an account record that the user does have access to, then this query on ContentDocumentLink would return the file data for the user:

SELECT Id, ContentDocument.LatestPublishedVersion.VersionData FROM ContentDocumentLink WHERE LinkedEntityId = :accountId

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm
Joseph PescatelloJoseph Pescatello
Thanks, Douglas, This query is exactly what I'v ebeen looking for.
SELECT Id, ContentDocument.LatestPublishedVersion.VersionData FROM ContentDocumentLink WHERE LinkedEntityId = :accountId