2

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

2

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
3
  • I am using Salesforce user not Guest user, still will your approach work? – Ajinkyasd Sep 8 '20 at 10:46
  • 1
    @Ajinkyasd While some of the information provided in this answer is relevant to enabling guest user file upload, the key piece of information for you is that the custom field name should end with fileupload__c. For SF users, you can specify the record-id, file-field-name and file-field-value in the HTML file, but ensure that you are providing correct value for fieldValue in the JS code and that the custom field name ends with fileupload__c. This will ensure the value is updated correctly. I had marked the custom field as not req & was able to work this out in my dev org. – arut Sep 8 '20 at 10:59
  • can we use file-field-name for authenticated user also? I tried but field didn't get any value. – Ankuli Mar 24 at 12:45
2

Special thanks to @arut for noting down the hack.

The custom field name should end with fileupload__c to make it work. After changing the field name and appending fileupload__c to it, uploaded files are updated with the value in file-field-value.

Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.