1

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);
    });
}
}

enter image description here

4
  • what errors do you have? Mar 10 at 13:35
  • there is nothing, just the salesforce pop up (Sorry to interrupt). i will post a picture of it in the post. this happens only after the files are uploaded, so the only thing that doesn't work is the record update.
    – Brian23Gt
    Mar 10 at 13:41
  • wrap the code inside the handleUploadFinished function in try-catch block and put console.log(error) inside catch block. probably there is an unhandled exception that you don't see Mar 10 at 13:44
  • i have done it : An error occurred while updating the record: Invalid recordInput
    – Brian23Gt
    Mar 10 at 13:54

1 Answer 1

1

You have several problems with your code.

First, you used:

@api myRecordId;

But then tried to refer to it as:

this.recordId

If this is a record-context aware component, then you must change:

@api myRecordId;

to:

@api recordId;

Otherwise, if this parameter is being passed in, you need to reference it as:

this.myRecordId;

Aside from that, your fields format is incorrect. The record Id must be passed in as part of the fields object, not as a separate parameter. You can see this in the documentation. In addition, using { fields: recordInput } actually causes the data to look like this:

{ fields:
  { fields:
    { [TREATMENT_EVENT_FIELD.fieldApiName]: true
    }
  }
}

Notice the extra layer of nesting. This won't work.

Most likely, you're getting the error because you specified the input as:

{ fields:
  { fields:
    { [TREATMENT_EVENT_FIELD.fieldApiName]: true
    }
  },
  recordId: this.recordId
}

And the API doesn't know how to handle the extra recordId parameter.

Instead, your data should look like:

{ fields: 
  { [TREATMENT_ID_FIELD.fieldApiName]: this.recordId,
    [TREATMENT_EVENT_FIELD.fieldApiName]: true
  }
}

To get there, you'll need the following changes:

import TREATMENT_ID_FIELD from '@salesforce/schema/Treatment_Event__c.Id';

...

  const fields = {};
  fields[TREATMENT_ID_FIELD.fieldApiName] = this.recordId;
  fields[TREATMENT_EVENT_FIELD.fieldApiName] = true;

  updateRecord({ fields })
5
  • I have done the changes but i still get the same error : Invalid recordInput I am using That's the updated version : @api recordId; import TREATMENT_ID_FIELD from '@salesforce/schema/Treatment_Event__c.Id'; import TREATMENT_EVENT_FIELD from '@salesforce/schema/Treatment_Event__c.Images_Uploaded__c'; const FIELDS = [ TREATMENT_EVENT_FIELD ]; const fields = {}; fields[TREATMENT_ID_FIELD.fieldApiName] = this.recordId; fields[TREATMENT_EVENT_FIELD.fieldApiName] = true; updateRecord({ fields })
    – Brian23Gt
    Mar 10 at 14:18
  • @Brian23Gt Context is important here. It sounds like you don't have an ID value loaded. What does console.log(JSON.stringify(fields)) look like?
    – sfdcfox
    Mar 10 at 14:24
  • not sure i got what you needed, i can't really see the log in the console but i see this : ReferenceError: fields is not defined
    – Brian23Gt
    Mar 10 at 15:11
  • i managed to fix it. the recordId was not assigned somehow, so i have changed the code like this: fields[TREATMENT_ID_FIELD.fieldApiName] = this.event.id; on top of that i learned how to debug properly! thanks a lot for your help
    – Brian23Gt
    Mar 10 at 15:21
  • 1
    @Brian23Gt You're welcome, glad I could help!
    – sfdcfox
    Mar 10 at 15:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .