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];
        // 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());  

2 Answers 2


We would have first created ContentVersion or ContentNote record. In my case, I have created ContentVersion as conVerRecord.

After that please make sure the below two points are checked while creating the **ContentDocumentLink** record.

ContentDocumentLink cdLink  = new ContentDocumentLink ();
  1. cdLink.ShareType = 'V'; See ShareType details
  2. cdLink.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVerRecord.Id].ContentDocumentId;

As in the 2nd point, ContentDocumentID is a reference field that will be dynamically populated/created upon creation of ContentVersion record.

So we will get the above error message if we just write like below:

cdLink.ContentDocumentId    = conVerRecord.ContentDocumentId;

So if we make sure these 2 points, this works well in Lightning Out (inside VF page) as well. I have tested it.


I have run the above code in Developer console, only change needs to do is cdl.ShareType ='V';

And it worked.

Content Note

If you open the Files tab in Lightning, you will able to see that ContentNote is attached to the Opportunity.

For more information, refer ContentDocumentLink

Share Type

V Viewer permission. The user can explicitly view but not edit the shared file.

C Collaborator permission. The user can explicitly view and edit the shared file.

I Inferred permission. The user’s permission is determined by the related record. For shares with a library, this is defined by the permissions the user has in that library.

  • I have tried all 3 permissions but still no luck. I am currently running this code on a Lightning Out app on a Salesforce Site. I still do not understand why that particular error comes out, it does not seem to make alot of sense. Please do advise on any possible solutions. I have tried create a note without using the ContentNote, no errors but no note is create after the code is run. Commented Jul 9, 2018 at 9:55
  • I just found out that my contentNote Id is NULL, maybe that is creating the error. Seems like my contentNote creation and insert is not working, any clues? Commented Jul 11, 2018 at 3:04

You must log in to answer this question.

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