[SalesForce] Examples of lightning-file-upload for Community Guest Users

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

Best Answer

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.