I currently have the ContentVersion information of a file I'm uploading, is there any way to find the Document Id using this info? Maybe through ContentDocumentLink or ContentDocument?

I wish to attain the Document ID to update the keywords field which is only associated with the Document object.


for (ContentDocumentLink cd: Trigger.new) {
    if (cd.ContentDocumentId != null) {
        cont.ContentDocumentId = cd.ContentDocumentId;
    System.debug('contentdocid = ' + cd.ContentDocumentId);
    cont1 = [SELECT VersionData FROM ContentVersion where ContentDocumentId =: cd.ContentDocumentId limit 1];

ContentDocumentId is different to the DocumentId in the Document object as this error led me to believe.

13:02:59:039 FATAL_ERROR System.QueryException: List has no rows for assignment to SObject

System.debug('contentdocid = ' + cd.ContentDocumentId);

This outputs

13:12:04:017 USER_DEBUG [24]|DEBUG|contentdocid = 06941000000vgsFAAQ

Which does not match with anything in the Document Object.

select Id from document where id = '06941000000vgsFAAQ'

0 rows.

Am I missing something?

  • As we can see from Content Objects data model, ContentDocumentId is a field on ContentVersion object
    – o-lexi
    Feb 13 '17 at 20:29
  • Edited, to give you more information.
    – JetSparx
    Feb 13 '17 at 21:15
  • select Id from contentDocument where id = '06941000000vgsFAAQ' is what you need - ContentDocument is correct object name
    – o-lexi
    Feb 13 '17 at 21:20
  • Thanks so much! As the Document keywords are what I'm trying to access, is there a definitive relationship between ContentDocument and Document? I know I could search by name, but to be sure I'm selecting the right file.
    – JetSparx
    Feb 13 '17 at 21:29
  • 2
    Read Differences Between Files, Salesforce CRM Content, Salesforce Knowledge, Documents, and Attachments to understand what is what, it looks like keywords are used with Documents, while you are uploading ContentDocuments
    – o-lexi
    Feb 14 '17 at 0:27

Content Document: It Represents a document that has been uploaded to a library in Salesforce CRM Content or Salesforce Files. The maximum number of documents that can be published is 30,000,000. This object record you don’t have to create. It gets created when you create ContentVersion which is the child of ContentDocument.

Content Version: Represents a specific version of a document in Salesforce CRM Content or Salesforce Files. In other words, this object stores document information similar like Attachment.

Now have a look on the relationship between these two objects below- enter image description here

Now ContentDocumentLink: This object will share the files with Users, Records, Groups etc. You can create multiple records to attach the same files under multiple records.

Some useful SOQL queries on Content Version and Content Document-

1.Get Content Document Id by Content Version Id

SELECT ContentDocumentId from ContentVersion where Id = '068......'

2.Get Content Version Id by Content Document Id

SELECT Id from ContentVersion where ContentDocumentId = '069......'

3.Get Download Number of Document by Version

SELECT Count(Id), ContentVersionId FROM ContentVersionHistory where field='contentVersionDownloaded' and ContentVersion.ContentDocumentId  = '069....'   group by ContentVersionId

4.Get Total Download Number for a Document

SELECT Count(Id) FROM ContentVersionHistory where field='contentVersionDownloaded' and ContentVersion.ContentDocumentId = '069....' group by ContentVersion.ContentDocumentId

That's all from my end. I got these from different resources. Thanks.


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.