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.

/**
 * @description       : 
 * @author            : Amit Singh
 * @group             : 
 * @last modified on  : 12-03-2020
 * @last modified by  : Amit Singh
 * Modifications Log 
 * Ver   Date         Author       Modification
 * 1.0   12-03-2020   Amit Singh   Initial Version
**/
@isTest
public with sharing class ContentDocumentUnitTest {

    @IsTest
    static void contentDocumentTest(){

        String firstname = 'Amit';
        String lastName  = 'Singh';

        Id profileId = [Select Id, Name From Profile Where Name = 'System Administrator' Limit 1].Id;
        Id roleId = null;

        String orgId = UserInfo.getOrganizationId();
        String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
        Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
        String uniqueName = orgId + dateString + randomInt;
        User userRecord = new User(  firstname = firstName,
            lastName = lastName,
            email = uniqueName + '@test' + orgId + '.org',
            Username = uniqueName + '@test' + orgId + '.org',
            EmailEncodingKey = 'ISO-8859-1',
            Alias = uniqueName.substring(18, 23),
            TimeZoneSidKey = 'America/Los_Angeles',
            LocaleSidKey = 'en_US',
            LanguageLocaleKey = 'en_US',
            ProfileId = profileId
        );
        if(!String.isBlank(roleId)){
            userRecord.UserRoleId = roleId;
        }

        Account accRecord = new Account(
            Name = 'The Pepsico Company',
            Rating = 'Hot'
        );

        insert accRecord;
        
        Test.startTest();
        System.runAs(userRecord){
            
            Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body to be insert in test class for testing the'); 
            
            ContentVersion contentVersion_1 = new ContentVersion(
                Title='SampleTitle', 
                PathOnClient ='SampleTitle.jpg',
                VersionData = bodyBlob, 
                origin = 'H'
            );
            insert contentVersion_1;
            
            ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId 
                            FROM ContentVersion WHERE Id = :contentVersion_1.Id LIMIT 1];
            
            ContentDocumentLink contentlink = new ContentDocumentLink();
            contentlink.LinkedEntityId = accRecord.id;
            contentlink.contentdocumentid = contentVersion_2.contentdocumentid;
            contentlink.ShareType = 'V';
            insert contentlink;

            System.assertEquals(contentlink.ContentDocumentId,contentVersion_2.ContentDocumentId );
        }
        Test.stopTest();
        
    }
}

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://www.sfdcpanther.com/how-to-preview-files-in-lightning-community-using-lwc/
  3. https://www.sfdcpanther.com/how-to-download-files-from-lightning-community-using-lwc/

Thanks for reading 🙂

#HappyReading #DeveloperGeeks #AskPanther #SFDCPanther

Our Score
Click to rate this post!
[Total: 1 Average: 5]

2 thoughts on “How to insert ContentDocument Using Apex in Salesforce”

Leave a Reply