1

I have a public community that allows a guest user to submit a form (LWC) and create a case record. I now want to embed the lightning:fileUpload component inside the form to allow them to attach a file to the case record. After testing, the case record is created, the ContentVersion record is also created, but is not linked to the case record.

Salesforce documentation does not elaborate on how to handle the logic to do that.

https://developer.salesforce.com/docs/component-library/bundle/lightning-file-upload/documentation

1
  • 1
    Without digging too deep, it appears a guest user can initiate a file upload (ContentVersion create), then hands it off to the system to actually create the record. By default, a guest user does not have access to the ContentVersion record. To associate the file to the case record, I wrote a trigger on ContentVersion object to create a ContentDocumentLink record, while passing the CaseId to the attribute specified on the SF documentation, a custom field created on ContentVersion object.
    – Tom
    Jan 22, 2021 at 14:42

2 Answers 2

3

As per doc,

By default, guest users can’t upload files and don’t have access to objects and their associated records.

To enable guest users to upload files, enable the org preference Allow site guest users to upload files. However, even if you enable this setting, guest users can’t upload files to a record unless guest user sharing rules are in place.

The Secure guest user record access org preference prevents access to records by guest users. As a result, if you specify the record-id in the lightning-file-upload component, the file fails to upload because the guest user doesn't have access to the record.

To enable guest users to upload files to a record, the org admin can create a custom field on the ContentVersion object. The field type can be text or picklist. The API name of the custom field must end with fileupload__c. For example, you can use the API name Guest_Record_fileupload__c for the custom field.

Specify the file-field-name and file-field-value attributes in lightning-file-upload to store a value in the custom field in the ContentVersion object. For example, set file-field-name to Guest_Record_fileupload__c. Set file-field-value to a value that can be used in Apex to associate the file to the record.

You can omit the record-id attribute when specifying file-field-name and file-field-value attributes. However, if you provide the record-id, file-field-name and file-field-value attributes, the record ID is ignored if the uploading user is a guest user.

Note that if you do not provide the record-id or file-field-name and file-field-value attributes, the uploaded file is private to an authenticated user.

This example specifies record-id, file-field-name and file-field-value attributes for behavior that supports authenticated users and guest users for uploading files to records.

<template>
    <lightning-file-upload
            label="Attach receipt"
            name="fileUploader"
            accept={acceptedFormats}
            record-id={myRecordId}
            file-field-name="Guest_Record_fileupload__c"
            file-field-value={encryptedToken}
            onuploadfinished={handleUploadFinished}
            multiple>
    </lightning-file-upload>
</template>
2

To associate a file (ContentVersion) to the Case record, I wrote an after insert trigger on the ContentVersion object to create a ContentDocumentLink record, associating the ContentVersion to the Case record. A custom field was created on ContentVersion object and on the HTML markup, the CaseId is passed to the attributes: file-field-name and file-field-value. So that when a ContentVersion record is created, the new custom field is populated, and is read by the trigger to pass to the ContentDocumentLink creation. On the front end side, the Case record would have to be created first in order to fetch the CaseId.

You must log in to answer this question.

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