How to insert ContentNote Using Apex in Salesforce

on

|

views

and

comments

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 Singhhttps://www.pantherschools.com/
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
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential