In lightning-file-upload component in LWC, I am trying to set one required custom picklist field value on the content version.
For this, I have used the below code:
<lightning-file-upload accept={acceptedFormats}
label="Attach sites"
onuploadfinished={handleUploadFinished}
record-id={recordId}
file-field-name="Custom_field__c"
file-field-value={fieldValue}></lightning-file-upload>
Here I have set file-field-name and file-field-value values, but when the file is uploaded, I get the option to select the field value and after selecting the value(on click of Next), the file gets uploaded.
If I make this field not required, then the file gets uploaded without selecting any option.
I am not using a guest user.
Documentation referred: https://developer.salesforce.com/docs/component-library/bundle/lightning-file-upload/documentation
Best Answer
usage of file-field-name and file-field-value attributes in lightning-file-upload
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 nameGuest_Record_fileupload__c
for the custom field.Specify the
file-field-name
andfile-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 toGuest_Record_fileupload__c
. Setfile-field-value
to a value that can be used in Apex to associate the file to the record.Coming to your case
You have defined both the
record-id
andfile-field-name
andfile-field-value
attributes in the lightning-file-upload.You can omit the
record-id
attribute when specifyingfile-field-name
andfile-field-value
attributes. However, if you provide therecord-id
,file-field-name
andfile-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
orfile-field-name
andfile-field-value attributes
, the uploaded file is private to an authenticated user.To make field required, You have these options:-