You are currently viewing Overview of Content Version, Content Document, And Content Document Link

Overview of Content Version, Content Document, And Content Document Link

Sharing is caring!

There is no denying the fact that Salesforce has its fingers in every pie of the customer experience. Salesforce, being a robust customer relationship management CRM software, offers an in-built content management system called Salesforce Content Management System that is intended to deliver a quality customer experience across multiple channels. The goal is to allow Salesforce users to easily manage and share meaningful content with prospects within the Salesforce environment.

In the Winter ’16 release, Salesforce introduced a new related list called “File” to better support notes and attachments across all platforms. Files that you upload to the Notes & Attachments related list on records in Salesforce Classic are now called Salesforce Files objects. Salesforce uses three objects: content document, content version, and content document link to store files data. Before you create files in Salesforce, you must understand object relationships.

In this blog post, we will be discussing Content Workspace, Content Document, Content Version, ContentDocumentLink, and some of the best tips and tricks to insert files in Salesforce using Apex. 

ContentWorkspace

It represents a public library in Salesforce Content. This object label as Library in a user interface with the critical prefix 058. This object is generally available in versions 17.0 and higher. This object does not apply to personal libraries. You cannot use API to create entries in this object.

SOQL sample: SELECT Id, Name, Description FROM ContentWorkspace

Content Document 

These are documents uploaded to a library in Salesforce CRM Content or Salesforce Files. This object also label as File in a user interface with key prefix 069. The maximum number of documents that can be uploaded is 30,000,000. You don’t have to create this object record. It automatically gets created when you create a ContentVersion, which is the child of a ContentDocument.

Content Version

It represents a specific version of a document in Salesforce CRM Content or Salesforce Files. This means that this object stores document information similar to Attachment. This object is known as File in a user interface with key prefix 068. The maximum size of the file to be uploaded via the SOAP API is 50 MB. Either document is published or downloaded via the API; it is first converted to base64 and then stored in VersionData. This conversion increases the document size by 37%. 

FileType –  When you upload documents, Salesforce determines file types: CSV, EXCEL, LINK, PDF, PNG, TEXT, WORD, ZIP, UNKNOWN. If filetype is a link, the ContentUrl field contains a URL, i.e., when you add a website link to the library.

FirstPublishLocationId: This field defines where the document will be published for the first time. It may be an object, user, or a library (which is ContentWorkspace).

ContentDocumentId & VersionId: When you load a document to ContentVersion, Salesforce will create a record in ContentDocument. However, if you upload a new version of the document, a new record will be created for ContentVersion, but not for ContentDocument.

ContentDocumentLink: This object will share the files with Users, Records, Groups, etc. You can create multiple records to attach the same files between various records. This object store files parent id in the LinkedEntityId field.

Salesforce ContentDocument and ContentVersion tips and tricks:

First trick: Get Content Document Id by Content Version Id

SELECT ContentDocumentId from ContentVersion where Id = ‘068……’

Second trick: Get Content Version Id by Content Document Id

SELECT Id from ContentVersion where ContentDocumentId = ‘069……’

Third trick: Get Download Number of Document by Version

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

Fourth trick: Get Total Download Number for a Document

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

Fifth trick: Get LinkedEntityId by ContentDocumentId

SELECT LinkedEntityId  FROM ContentDocumentLink WHERE ContentDocumentId = ‘0689……’

Code: Insert Files in Salesforce Using Apex

Create Document Parent Record

Account acc = new Account(Name=’Test Account’);

Insert acc;

Create Document

ContentVersion cv = new ContentVersion();

cv.Title = ‘Test Document’;

cv.PathOnClient = ‘TestDocument.pdf’;

cv.VersionData = Blob.valueOf(‘Test Content’);

cv.IsMajorVersion = true;

Insert cv;

Get Content Documents

Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;

Create ContentDocumentLink

ContentDocumentLink cdl = New ContentDocumentLink();

cdl.LinkedEntityId = acc.Id;

cdl.ContentDocumentId = conDocId;

cdl.shareType = ‘V’;

Insert cdl;

Code: Convert Attachment to File in Salesforce Using Apex

Here is the code to move attachment documents to file using apex.

Get Attachment

Attachment attach = [SELECT Id, Name, Body, ContentType, ParentId From Attachment LIMIT 1];

Insert ContentVersion

ContentVersion cVersion = new ContentVersion();

cVersion.ContentLocation = ‘S’; //S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork.

cVersion.PathOnClient = attach.Name;//File name with extention

cVersion.Origin = ‘H’;//C-Content Origin. H-Chatter Origin.

cVersion.OwnerId = attach.OwnerId;//Owner of the file

cVersion.Title = attach.Name;//Name of the file

cVersion.VersionData = attach.Body;//File content

Insert cVersion;

After saved the Content Verison, get the ContentDocumentId.

Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId;

Insert ContentDocumentLink

ContentDocumentLink cDocLink = new ContentDocumentLink();

cDocLink.ContentDocumentId = conDocument;//Add ContentDocumentId

cDocLink.LinkedEntityId = attach.ParentId;//Add attachment parentId

cDocLink.ShareType = ‘I’;//V – Viewer permission. C – Collaborator permission. I – Inferred permission.

cDocLink.Visibility = ‘InternalUsers’;//AllUsers, InternalUsers, SharedUsers

Insert cDocLink;

With Salesforce CRM Content, you can effortlessly organize, share, search, and manage content within your organization. Salesforce CRM Content includes all file types, from traditional business documents such as Microsoft PowerPoint presentations to audio files, video files, Web pages, and google docs.

If you want to explore the powerful CRM software features and get the most from your Salesforce implementation, contact Cloud Analogy, the #1 Salesforce Implementation partner, now!

Akshay

Akshay Dhiman

Chief Technical Officer
Akshay Dhiman, the CTO of Cloud Analogy, has been a standout and successful Salesforce Platform Developer for years. He has a rich experience in Salesforce Integration, JavaScript, APEX, VisualForce, Force.com Sites, Batch Processing, Lightning, PHP, C++, Java, NodeJs, ReactJs, Angular 8, GraphQL, React Native, Web Technology, and jQuery.

Hire the best Salesforce Development Company. Choose certified Salesforce Developers from Cloud Analogy now.

Leave a Reply

× How can I help you?