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;
n.body=inputArray;
n.title='Array';
n.isPrivate=false;
insert n;