TransWikia.com

Relationship between ContentVersion object and Document object

Salesforce Asked by JetSparx on December 10, 2021

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.

Edit:

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?

One Answer

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.

Answered by Sam on December 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP