How to insert ContentDocument Using Apex in Salesforce

Hi Everyone,

In this post, we will see how we can insert the Content Document or Content Version in Apex Test class.

Content Document: – Represents a document that has been uploaded to a library in Salesforce CRM Content or Salesforce Files.

Content Version: – Represents a specific version of a document in Salesforce CRM Content or Salesforce Files. One Content Document can have multiple content versions.

Content Document Link:- Represents the link between a Salesforce CRM Content document or Salesforce file and where it’s shared. A file can be shared with other users, groups, records, and Salesforce CRM Content libraries.

Files in Salesforce
Files in Salesforce

Here is the code for the test class.

ContentVersion – Contains the complete information about the Content Document. Which have the following field.

  1. Title – The title of a document.
  2. PathOnClient – The complete path of the document. One of the fields that determine the FileType.
  3. VersionData – The content of the file/document.
  4. Origin – The source of the content version. Valid values are:
  • C—A content document from the user’s personal library. The label is Content. The FirstPublishLocationId must be the user’s ID. If FirstPublishLocationId is left blank, it defaults to the user’s ID.
  • H—Salesforce files from the user’s My Files. The label is Chatter.

References

  1. https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm
  2. https://testwebgrantha.xyz/sfdc/how-to-preview-files-in-lightning-community-using-lwc/
  3. https://testwebgrantha.xyz/sfdc/how-to-download-files-from-lightning-community-using-lwc/

Thanks for reading 🙂

#HappyReading #DeveloperGeeks #AskPanther #SFDCPanther

Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading