I have a test class that is inserting a ContentDocumentLink between a ContentNote (SNOTE) and a custom object. Everytime I try the insert it causes an Internal Salesforce Error. I have absolutely narrowed it down to the insert of the ContentDocumentLink.

ContentNote cn = new ContentNote(Title='Test', Content= Blob.valueOf('Test'));
    insert cn;
List<ContentVersion> cvList = new List<ContentVersion>();
    cvList = [SELECT Id, FileType, ContentDocumentId FROM ContentVersion];
    ContentVersion cv = cvList[0];
    System.debug('SCH cvList '+cvList);
    ContentDocumentLink cdl = new ContentDocumentLink(ContentDocumentId = cv.ContentDocumentId,
                                                      LinkedEntityId = m.Id,
                                                      shareType = 'V');

    insert cdl;

I have tried a variety of other things, using the Id directly from the ContentNote or from a queried ContentDocument, neither of which works. I have verified the ContentVersion is an SNOTE so it created everything correctly it looks like, but it will not allow the insert of the ContentDocumentLink or give any type of a usable error message.

  • I had this a while back and I never manged to resolve it... I tried a lot of stuff too. I wonder if it's a bug Mar 11 '20 at 22:33

I was running this code in a customer sandbox. I tested it in my personal dev org and found the following.

Creating a ContentNote automatically creates a ContentVersion, ContentDocument and ContentDocumentLink record. The ContentDocumentLink record is between the ContentDocument and the User that creates it.

When I ran the code in my own org it ran fine. On the suggestion of a colleague I created a new sandbox to test this in, off of the same org it had failed in earlier. The new sandbox allowed it to run without error. The sandbox it failed in was a Full Copy and the one it works in is a Dev Sandbox.

So whatever this was it was an org specific problem that went away when changing orgs. If you run in to it I would suggest trying to create a new sandbox. If that doesn't work I think a ticket with Salesforce would be the next step.

Edit to add: After further testing using some custom objects for the LinkedEntityId caused the Internal Salesforce Error to reoccur. Luckily I had enough objects to choose from I was able to reach over 75% coverage, but there is something quirky going on with inserting these records in a test class.

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.