I am receiving this error when I try to create a Note and inserting it to an opportunity. The opportunity is valid and there is no documentation of requiring the field, "document" for ContentDocumentLink. Why is this error appearing and causing my insert to fail?
Error: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Document]: [Document]
The following is my code:
Opportunity opp = [SELECT Id FROM Opportunity WHERE Id =:accountId];
try
{
// Step 1, create new Note
ContentNote cn = new ContentNote();
// give the note a subject
cn.Title = 'This is Title of the Note!';
// sf content blobs don't like some html characters, is fine with some others
cn.Content = Blob.valueOf('test');
// insert note, later we'll connect it to the record
insert cn;
// Step 2, create the ContentDocumentLink
ContentDocumentLink cdl = new ContentDocumentLink();
// link the link object to the note
cdl.ContentDocumentId = cn.Id;
// link to sObjects by Id
cdl.LinkedEntityId = opp.Id;
// set visibility
cdl.Visibility ='AllUsers';
// set ShareType
cdl.ShareType ='C';
// insert the content link
insert cdl;
}
catch(Exception ex)
{
return ('Error: ' + ex.getMessage() + ' // ' + ex.getLineNumber() + ' // ' + ex.getStackTraceString());
}