I am trying to understand the relation between documents and contentversion. When i queried documents, it returned 100+ records but when i queried contentversion it returned only 1. Please help me understand the relation between them. Plan is to move to lightning so doing some groundwork. Also i read to write some trigger on documents write it on contentversion. How these 2 are related?
[SalesForce] Document and ContentVersion
Related Solutions
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-
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.
To solve your purpose trigger needs to be invoked on ContentDocumentLink
.
When you are uploading the file from File Tab trigger will fire on "ContentVersion" object, but when you are uploading file from Chatter tab you need to write trigger on "ContentDocumentLink" object.Then only you will be able get the Content Document Id.
It seems like a standard feature of salesforce that the triiger on contentVersion doesn't get called when you are uploading the file from File Tab.
Best Answer
Differences Between Files, Salesforce CRM Content, Salesforce Knowledge, Documents, and Attachments
There is no relation between these 2 objects.
Moving Documents to Salesforce Files