1

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.

3
  • 1
    Do you have a Trigger on Content Document Link Object in your org ? Commented Jan 15, 2018 at 16:48
  • Holy grail, I had. I deactivated the trigger it works. Its strange, the trigger only creates chatter feeds which in turn used to update opportunity. So it looks like when it tried to update opportunity record it failed. Commented Jan 15, 2018 at 16:55
  • let me add that as an answer !!! Commented Jan 15, 2018 at 16:55

2 Answers 2

2

Confirmed from Salesforce Support and Salesforce R&D that this is "working as designed" and also confirmed that deactivating/removing the trigger will not cause this error. Just posting in case anyone comes across this looking for hope/answers

UPDATE:

If you can deactivate or remove the CDL Trigger and dont foresee any use for a CDL trigger, go for it; however if that's not an option another workaround would be to continue saving as attachment(attachments still save on anonymous), with the attachment tagged for processing (tag found in description) . Batch job running on an admin context pulling out tagged attachments and creating the content version, content document and content document links asynchronously. Know it sounds like a Rube Goldberg solution but explored all avenues and seems to be the only one to work (@futures wont work as the running context for those will still be the guest user)

1
  • Not exactly. What they confirmed was the behaviour wherein if there exists a trigger on the ContentDocumentLink, even without any code on the trigger, the exception still occurs. Removing the trigger somehow fixes the issue, really weird behaviour if you ask me. Commented Jan 23, 2019 at 3:25
2

Looks like this issue happens because by default salesforce has a Trigger On Content Document Link Record .

When you insert Content Document Link these triggers run and cause issues as it tries to update Opportunity as guest user profile if your logic is inside a class with keyword "with sharing".

Deactivate this trigger to fix the issue

8
  • If I deactivate trigger via Metadata it works, If I deactivate it via handler pattern or just delete the content of trigger, it still gives me error. Commented Jan 15, 2018 at 17:05
  • 1
    hmm -- normally Sites users can update Opportunity if class is without sharing (by default, Sites guest users can't update Opportunity -only Read). If the trigger in question uses a class without sharing to update Opportunity, I'll bet this will work.
    – cropredy
    Commented Jan 15, 2018 at 17:05
  • @PranayJaiswal That sounds like different issue all together . DO you need the trigger ? If no and you delete whats the error ? Commented Jan 15, 2018 at 17:06
  • @cropredy Thanks .Thats a great point .Added to the answer Commented Jan 15, 2018 at 17:07
  • Updated problem statement. Looks it has more to do with trigger . Commented Jan 15, 2018 at 17:12

You must log in to answer this question.

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