How to insert ContentNote Using Apex in Salesforce

Hello #Ohana,

As we know that Salesforce is migrating to Lightning and in Salesforce Lightning they are using Enhanced Notes which are also note as ContentNote.

If you want to know how to insert a Content Document using Apex. Check here.

In this post, we will learn how to create Content Note in Salesforce. There are 2 different approaches which we can use to create the Content Note.

Approach 1

/* 
            Approach 1 :
            Use FileExtension as  '.sonte' when creating the Content Note using ContentVersion Object.
        */

        ContentVersion objCntNote = new ContentVersion();
        objCntNote.Title = 'Test Content Note by pantherschools.com';
        objCntNote.PathOnClient = objCntNote.Title + '.snote';
        objCntNote.VersionData = Blob.valueOf('Test Content Note by pantherschools.com');
        objCntNote.FirstPublishLocationId = '0010o00002KIY2KAAX';  // ParentId
        insert objCntNote;
        System.debug('Content Note Id **** \n '+objCntNote);

Approach 2

Create a Content Note Record and then Content Document Link record to Link the note with Salesforce record.

For this approach, we need to enable the Enhanced Notes in Salesforce. To enable, Setup -> Notes Settings -> Enable Notes

Enable Enhanced Notes in Salesforce

Example Code.

ContentNote noteRecord = new ContentNote();
noteRecord.Title   = 'Test Content Note by pantherschools.com Approach 2';
String body        = 'Test Content Note by pantherschools.com Approach 2';
noteRecord.Content = Blob.valueOf(body.escapeHTML4());
insert noteRecord;
ContentDocumentLink link = new ContentDocumentLink();
link.ContentDocumentId   = noteRecord.id;
link.LinkedEntityId      = '0010o00002KIY2KAAX';
link.ShareType           = 'V';
link.Visibility          = 'InternalUsers';
insert link;
ContentNote in Salesforce

Here is the Complete code

/**
 * @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 ContentNotesUtils {
    
    @IsTest
    public static void contentNoteTest(){

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

        insert accRecord;
        

        /* 
            Approach 1 :
            Use FileExtension as  '.sonte' when creating the Content Note using ContentVersion Object.
        */

        ContentVersion objCntNote = new ContentVersion();
        objCntNote.Title = 'Test Content Note by pantherschools.com';
        objCntNote.PathOnClient = objCntNote.Title + '.snote';
        objCntNote.VersionData = Blob.valueOf('Test Content Note by pantherschools.com');
        objCntNote.FirstPublishLocationId = accRecord.Id;  // ParentId
        insert objCntNote;
        System.debug('Content Note Id **** \n '+objCntNote);
        

        ContentNote noteRecord = new ContentNote();
        noteRecord.Title   = 'Test Content Note by pantherschools.com Approach 2';
        String body        = 'Test Content Note by pantherschools.com Approach 2';
        noteRecord.Content = Blob.valueOf(body.escapeHTML4());
        insert noteRecord;

        Test.startTest();
            ContentDocumentLink link2 = new ContentDocumentLink();
            link2.ContentDocumentId   = noteRecord.id;
            link2.LinkedEntityId      = accRecord.Id;//userInfo.getOrganizationId();
            link2.ShareType           = 'V';
            link2.Visibility          = 'AllUsers';
            insert link2;
        Test.stopTest();
        
    }
    
}

Thanks for reading 🙂 Sharing is Caring 🙂

#HappyReading #DeveloperGeeks #AskPanther #SFDCPanther

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/
  4. https://testwebgrantha.xyz/sfdc/how-to-insert-contentdocument-in-apex-class/
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

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