Loading

Error inserting ContentNote/ContentDocumentLink/ContentDocument as a Guest User in Community

Дата публикации: Oct 13, 2022
Описание
Issue:
You will see this in the Guest User Debug Logs:

REQUIRED_FIELD_MISSING, Required fields are missing: [Document]: [Document]

when trying to insert a ContentNote/ContentDocumentLink via Apex in a community. 

This will occur, even when perm:

"Allow site guest users to upload files" in Setup| Customize | Salesforce Files | Settings | General Settings
is enabled. 

Example Code:
<apex:page controller="CreateContentDocumentController">
    <apex:form >
        <apex:pageMessages />
        <apex:commandbutton action="{!doInsert}" value="Insert" />
    </apex:form>
</apex:page>
 
public without sharing class CreateContentDocumentController {
public id CDId;

  public void doInsert(){
        ContentNote cn = new ContentNote();
        cn.Title = 'TestTest';
        String body = 'Hello World.Test';
        cn.Content = Blob.valueOf(body.escapeHTML4());
        insert(cn);
        System.debug('cn.Id'+cn.Id);
        CDId = cn.Id; 
        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.put('LinkedEntityId', 'AccountId'); //AccountId
        cdl.put('ContentDocumentId', cn.Id);
        cdl.put('ShareType', 'V');
        cdl.put('Visibility', 'AllUsers');
        insert cdl; 
        
    } 
        
  
}

If you add this visualforce page and controller to a Community, when you click the insert button with debug logs enabled you can see an error. 
 
Решение
In order to not receive an error with Inserting a ContentNote/ContentDocument/ContentDocumentLink as a Guest User in a Community, it is required for the following to be enabled:

Setup | All Communities | Workspaces | Administration | Preferences | "Let guest users view asset files and CMS content available to the community"

NOTE: When your community is setup so that it can be publicly accessed, this preference is enabled by default. However, as many customers may disable any preferences which they think might be unnecessary, it's possible that you may have disabled this preference. 

PREFERENCE DOC:
https://help.salesforce.com/articleView?id=community_builder_page_access_settings.htm&type=5
If public access is enabled in Experience Builder at the page or community level, the Let guest users view asset files and CMS content available to the community preference is enabled in Administration | Preferences in Experience Workspaces or Community Management. This preference lets guest users view asset files shared with the community on publicly accessible pages. It remains enabled as long as the page has public access enabled.

As Asset Files in a Community usually refer to something different than ContentNote/ContentDocument/ContentDocumentLink objects, this article is created to clarify the reasons why you may need to keep this preference enabled even when you are not using asset files on a community. 


In order for guest users to insert or update ContentNote/ContentDocument/ContentDocumentLink objects in a Community, this preference needs to be enabled. 


NOTE: It is not possible to insert ContentNote/ContentDocument/ContentDocumentLink objects in a Site. This is only possible via preference set in a community. 
Номер статьи базы знаний

000380693

 
Загрузка
Salesforce Help | Article