I have a requirement where I have to provide functionality to Anonymously upload File to Opportunity.
I want to attach their uploaded content as Files instead of Attachment.
I am able to create contentVersion, but as soon as I try try to create ContentDocumentLink it throws an exception.
Please find the below code:
<apex:page controller="MySitePageController" showHeader="false"
standardStylesheets="false">
<apex:form>
<apex:inputFile value="{!file}" fileName="{!fileName}"/>
<apex:commandbutton action="{!upload}" value="Upload" />
</apex:form>
</apex:page>
Apex Code:
public without sharing class MySitePageController {
String encryptedRecId{set;get;}
public MySitePageController() {
encryptedRecId=ApexPages.currentPage().getParameters().get('id');
}
public blob file { get; set; }
public String fileName{ get; set; }
public PageReference upload() {
ContentVersion v = new ContentVersion();
v.versionData = file;
v.title = fileName;
v.pathOnClient =fileName;
insert v;
System.debug(v.id);
v=[Select id,ContentDocumentID from ContentVersion where id=:v.id];
ContentDocumentLink cl = new ContentDocumentLink();
cl.ContentDocumentId = v.ContentDocumentId;
cl.LinkedEntityId = encryptedRecId;
cl.ShareType = 'V';
cl.Visibility = 'AllUsers';
insert cl;
return null;
}
}
Trigger Code:
trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert,before insert) {}
** Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []**
Can someone point me what can be the issue? I have given permissions of read Files and Opportunity on that Guest Site user profile?
Update: As suggested by Mohit S, I tried deactivating ContentDocumentLinktrigger from metadata, it worked fine. Next i tried to do same using Trigger handler pattern same error.
Finally I tried to test by deleting the code of ContentDocumentLink Trigger and still it is causing the same problem.
I am able to update opportunity inside the controller, thus I believe its not the issue.
We have so many functionality built in ContentDocumentLinkTrigger that it is impossible for me to deactivate it in our live implementation.
Tried in Winter18 and Spring18, doesnt look like its an Release issue.