0

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.Title=title;
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..

Thanks..

3
  • Its not Note, it is ContentNote. Note is older version, which is succeeded by ContentNote
    – Mahmood
    Mar 17, 2020 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, 2020 at 15:03
  • Please see my answer.
    – Mahmood
    Mar 17, 2020 at 15:08

2 Answers 2

3

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;
insert(cn);

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';
insert(cdl);

For details on ContentNote please refer to the documentation

1
  • 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, 2020 at 17:03
1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.