Content Note in Apex Class

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 sfdcpanther.com';
        objCntNote.PathOnClient = objCntNote.Title + '.snote';
        objCntNote.VersionData = Blob.valueOf('Test Content Note by sfdcpanther.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 sfdcpanther.com Approach 2';
String body        = 'Test Content Note by sfdcpanther.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 sfdcpanther.com';
        objCntNote.PathOnClient = objCntNote.Title + '.snote';
        objCntNote.VersionData = Blob.valueOf('Test Content Note by sfdcpanther.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 sfdcpanther.com Approach 2';
        String body        = 'Test Content Note by sfdcpanther.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://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/
  4. https://www.sfdcpanther.com/how-to-insert-contentdocument-in-apex-class/
Our Score
Click to rate this post!
[Total: 1 Average: 5]

Leave a Reply