I have a requirements that I need to share files with particular user which belongs to partner community user through trigger/apex code. I am using the below hard to test. But finding some issue.

 ContentDocumentLink cdl = new ContentDocumentLink();
 cdl.ContentDocumentId = '0690q000000oiuNAAQ';
 cdl.ShareType = 'V';
 cdl.Visibility = 'AllUsers';
 cdl.LinkedEntityId = '0050q000002iRF1';
 insert cdl;

Error:EXCEPTION: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.: [LinkedEntityId]

Please help in this!

  • when is that code running? Through the trigger on some object, through a button click, through a custom page action, etc. Commented Nov 2, 2021 at 14:22
  • @KrisGoncalves , I am running in Workbench.
    – SFDC 44
    Commented Nov 2, 2021 at 14:26
  • @KrisGoncalves, Also I tried with before insert trigger on ContentDocumentLink. But did not work
    – SFDC 44
    Commented Nov 2, 2021 at 14:35

1 Answer 1


I know about this error -


this is caused by the fact that the record you are trying to access is not owned by you. or it doesn't exist. So this could happen - when you created the actual record to which this file is attached to, you owned that. But some automation process/ or by any other means its owner would have changed to some other user or some queue.

so to resolve that, you need to be either in the same queue. Or at the highest hierarchy in the role, in compared to users added to that queue.

Hope I have explained to the best of your understanding.

You must log in to answer this question.

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