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.
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
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.
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’);
ContentVersion cv = new ContentVersion();
cv.Title = ‘Test Document’;
cv.PathOnClient = ‘TestDocument.pdf’;
cv.VersionData = Blob.valueOf(‘Test Content’);
cv.IsMajorVersion = true;
Get Content Documents
Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = acc.Id;
cdl.ContentDocumentId = conDocId;
cdl.shareType = ‘V’;
Code: Convert Attachment to File in Salesforce Using Apex
Here is the code to move attachment documents to file using apex.
Attachment attach = [SELECT Id, Name, Body, ContentType, ParentId From Attachment LIMIT 1];
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
After saved the Content Verison, get the ContentDocumentId.
Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId;
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
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.