I have an LWC Component and i am trying to update the field Images_Uploaded__c to true once the lightning input has finished the upload. I would like to do it in the component without having to modify the apex controller, but i keep getting errors after the upload and i don't know where to look anymore.
HTML :
<lightning-file-upload
label="Upload"
name="fileUploader"
accept={acceptedFormats}
record-id={event.id}
onuploadfinished={handleUploadFinished}
multiple></lightning-file-upload>
JS :
import { LightningElement, api, track, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { updateRecord } from 'lightning/uiRecordApi';
import TREATMENT_EVENT_FIELD from '@salesforce/schema/Treatment_Event__c.Images_Uploaded__c';
const FIELDS = [
TREATMENT_EVENT_FIELD
];
import TREATMENT_EVENT_OBJECT from '@salesforce/schema/Treatment_Event__c';
export default class TreatmentEventInfo extends LightningElement {
@api event;
@api myRecordId;
get acceptedFormats() {
return ['.pdf', '.png' ,'.jpeg','.jpg','.heic'];
}
handleUploadFinished(event) {
const uploadedFiles = event.detail.files;
const toastEvent = new ShowToastEvent({
title: 'Success!',
message: 'Number of files uploaded: ' + uploadedFiles.length,
variant: 'success',
mode: 'sticky',
duration: 2000
});
this.dispatchEvent(toastEvent);
const fields = {};
fields[TREATMENT_EVENT_FIELD.fieldApiName] = true;
const recordInput = {
fields
};
updateRecord({ fields: recordInput, recordId: this.recordId })
.then(() => {
const toastEvent = new ShowToastEvent({
title: 'Success!',
message: 'Record updated successfully',
variant: 'success'
});
this.dispatchEvent(toastEvent);
})
.catch(error => {
console.error(JSON.stringify(error));
const errorEvent = new ShowToastEvent({
title: 'Error!',
message: 'An error occurred while updating the record: ' + error.message,
variant: 'error'
});
this.dispatchEvent(errorEvent);
});
}
}