0

I am trying to refactor some Apex code that currently adds an Attachment to a custom object, so that it uses ContentVersion and ContentDocumentLink.

When I insert the ContentDocumentLink, an error is thrown with the message

You cannot create a link for this type of entity through the api

However, the ContentDocumentLink object is being created, and I am able to see it in the Files related list, so it is strange that I am getting an error at all.

Does anyone know what the issue is? My code is shown below:

ContentVersion conVer = new ContentVersion();
conVer.ContentLocation = 'S'; // In Salesforce
conVer.Origin = 'H';
conVer.PathOnClient = fileName;
conVer.Title = fileName;
conver.VersionData = Blob.valueOf(csv);
conver.FirstPublishLocationId = parentRecord.Id;
insert conVer;

Id conDocId = [Select ContentDocumentId From ContentVersion Where Id = :conVer.Id].ContentDocumentId;

// Create ContentDocumentLink
ContentDocumentLink link = new ContentDocumentLink();
link.ContentDocumentId = conDocId;
link.LinkedEntityId = parentRecordId;
link.ShareType = 'I'; // Inferred permission
link.Visibility = 'AllUsers';
insert link; // This is where the error is being thrown.
2

There's some context that's missing from here (such as if this code is being called from within a Community or if it is being called from a Site.com page, etc), but from what I can see, it isn't even necessary to do what you're doing at the moment.

When you're creating the ContentVersion, you're assigning parentRecord.Id to FirstPublishLocationId

conver.FirstPublishLocationId = parentRecord.Id;
insert conVer;

When you create a ContentVersion in this manner, Salesforce will automatically create a ContentDocument for you (which you're already aware of since you're querying the ContentDocumentId in the next line) and the default ContentDocumentLink for you. By default, a ContentDocumentLink with LinkedEntityId set to the logged in User's Id is created. But there will also be one created for the FirstPublishLocationId if it is specified when the ContentVersion is first inserted (again, context is needed here as FirstPublishLocaitonId cannot be set in certain scenarios).

Because of this, you actually cannot insert a new ContentDocumentLink for the same parent record as the one that was assigned to the FirstPublishLocationId as one will already exist in the system. I cannot speak for that api error specifically (as it may still throw that error for a different record due to the context in which you're calling it), but in this case, it isn't necessary to even try to create the ContentDocumentLink manually (or rather, it will throw an error because of the circumstances).

  • Fantastic. Thanks @Roti. I had spent all day on that and I just didn't understand what the docs were telling me, but this helped to put it in context, and it's now working as expected. :) – lemming Oct 26 at 10:10
  • @lemming glad to hear that :) – Roti Oct 29 at 15:40

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.