I am trying to insert a new ContentNote and link it to an opportunity with ContentDocumentLink. However, after inserting the content note, the ID returns as NULL, so referencing it in the ContentDocumentLink causes an error. What may be the issue here?

// Step 1, create new Note          
ContentNote cn = new ContentNote(
  Title = 'Title',
  Content = Blob.valueOf('Dummy')
insert cn; //After this insert, cn.Id is NULL

// Step 2, create the ContentDocumentLink
ContentDocumentLink cdl = new ContentDocumentLink();           
cdl.ContentDocumentId = cn.Id; //Referencing cn.Id causes error
cdl.LinkedEntityId = opp.Id;          
cdl.Visibility ='AllUsers';
cdl.ShareType ='V';

insert cdl;

Inserting a classic Note has no issues at all.

note n = new note();
n.parentId= opp.Id;
insert n;
  • First question, have you enabled the Enhanced Notes Feature? Also, I think you'll want the ContentNote.ContentPreview which is what is generated to display from the contentDocumentLink. Check the Object Reference for more as this is just from memory.
    – crmprogdev
    Jul 11, 2018 at 6:05
  • Yes, it is enabled. I am able to update an existing ContentNote. The main problem now is the insertion of the new ContentNote does nothing, hence returning NULL for the ID reference. I checked the database and it is not inserted. No errors too. Jul 11, 2018 at 8:17

1 Answer 1


It appears to me from looking at the Object Reference for ContentNote the issue is with your method for inserting the note.

They give two examples and both show discrepancies with how you're creating the content's body which has to do with escaping the HTML from the enhanced note. Below are the two relevant examples.

Example 1:

ContentNote cn = new ContentNote();
cn.Title = 'test1';
String body = 'Hello World. Before insert/update, escape special characters such as ", ', &, and other standard escape characters.';
cn.Content = Blob.valueOf(body.escapeHTML4());

Example 2:

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;

I recommend you give one of the above methods a try and see if it doesn't resolve your issue.

  • I believe the documentation is wrong to recommend escapeHTML4(). escapeXML() mostly works, but with unexpected caveats.
    – David Reed
    Jul 11, 2018 at 14:13
  • @DavidReed Again, this is for enhanced notes which are often recorded in mobile devices. In that context, escaping HTML4 would make sense. It's not a rich text editor.
    – crmprogdev
    Jul 11, 2018 at 14:24
  • Thanks for the reply. I tried both methods and still, does not work. Both insertions provide no error and does not insert into the database at all. However, if I already created a Content Note before, I have no issues referencing the Content Note ID and updating it. The only problem seems to be with inserting a new Content Note. Jul 12, 2018 at 3:40
  • Based on the issues you're encountering, I recommend you check to make certain your RunAs user has create permissions on ContentNote. It sounds to me as though they may not and instead may only have READ, EDIT.
    – crmprogdev
    Jul 12, 2018 at 4:55
  • That makes sense. The user for this is using a Guest Profile. May I know if there is a way to provide EDIT access to the ContentNote for Guest Profile? As far as I know, I have looked around at the permissions and there is no ContentNote permissions. Furthermore, I don't think Guest Profiles can be assigned with EDIT permissions? Jul 12, 2018 at 6:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .