[SalesForce] Set required field value in lightning-file-upload

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.enter image description here

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 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.

Coming to your case

You have defined both the record-id and file-field-name and file-field-value attributes in the lightning-file-upload.

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.

To make field required, You have these options:-

  1. You can mark field as required
  2. Write a trigger on ContentVersion object to throw an error if field is blank
  3. Create a validation rule to throw an error if field is blank
Related Topic