I want to save a new Note to a Lead via apex.

I create a new Note :

Note n = new Note();
n.Body = body;
n.ParentId = Id.valueOf(s); // Lead Id
n.IsPrivate = false;
Insert n;

But then, Its Save the Note under "Note&Attachment" related list and not under Note related list...

I know it should be super simple to rich out the answer in the internet.. But I didn't find it...

Links and answers will be much appreciated..


  • Its not Note, it is ContentNote. Note is older version, which is succeeded by ContentNote – Mahmood Mar 17 at 15:01
  • I saw ContentNode but i didnt understand how to relate it with the Lead... which field should have the LeadId? – Salvation Mar 17 at 15:03
  • Please see my answer. – Mahmood Mar 17 at 15:08

What you are doing is inserting Note, which is taken over by ContentNote and to insert ContentNote you simply need to do the following

ContentNote cn = new ContentNote();
cn.Title = 'test2';
String body = '<b>Hello World. Because this text is already formatted as HTML, it does not need to be escaped. 
Special characters such as &quot;, etc. must already use their HTML equivalents.</b>';
cn.Content = body;

and then link it to the Lead record

ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = cn.id; 
cdl.LinkedEntityId = 'ID of Lead';
cdl.ShareType = 'C';
cdl.Visibility = 'AllUsers';

For details on ContentNote please refer to the documentation

| improve this answer | |
  • It is not worked for me, but I found out it doesn't because it ran by a guest user over a force.com site and there is a permission issue. When I run it from an anonymous window (dev console) It worked. So you answer my question. Thanks. – Salvation Mar 17 at 17:03

You are creating Classic Notes. Classic Notes (the Note sObject) are available in Lightning Experience in the Notes & Attachments related list.

Enhanced Notes (ContentDocument/ContentVersion/ContentNote sObjects) are shown in the Notes related list in Lightning Experience.

This is described in Trailhead:

Enhanced notes and classic notes don’t play well together. So the enhanced Notes that you create for a record are located in the Notes related list. Any notes that you previously created for the record using the classic notes tool are in the Notes & Attachments related list.

To switch to Enhanced Notes, use the ContentNote sObject, with code as described in Mahmood's answer.

| improve this answer | |

Your Answer

