1

How to update from updateRecord UIAPI in lWC for List of Arrays

I have a lwc component which returns an Array of idString and i would need to pass it to parameter for listofRecords which would check for all the elements and update the name value to us account and update in bulk

can't use apex because of performance and server calls need to use lwc updaterRecord but it's not updating the record Is there any approach wrong in my code


const COLS = [
    {label:'Id', fieldName:'Id'},
    {label:'name', fieldName:'name'},
];

idString =["1234","4567","7890","2345"];

  updateContactRecordus(listOfrecords) {
        let promises = [];
        listOfrecords.forEach(idsString => {
            let fields = {
                Name: 'US Account'
            };
            console.log('Value of fields is'+ fields);

            promises.push(updateRecord({ fields }));
        });
        Promise.all(promises)
        .then(result => {
            this.showToast('Success', 'Records updated successfully', 'success');
            // Optionally, handle any post-update logic here
        })
        .catch(error => {
            this.showToast('Error updating records', error.body.message, 'error');
        });
}

showToast(title, message, variant) {
    const evt = new ShowToastEvent({
        title: title,
        message: message,
        variant: variant
    });
    this.dispatchEvent(evt);
}
5
  • You need to include the field: Id in your fields object Commented Jul 19, 2024 at 16:19
  • Adding a id field as well still the values are not updated Commented Jul 20, 2024 at 2:01
  • What do you mean "can't use apex because of performance"? What's the performance issue you're trying to avoid? Commented Jul 22, 2024 at 1:39
  • We are trying to avoid server calls and make use the client side caching Commented Jul 22, 2024 at 5:27
  • But why? what's wrong with server calls? Wouldn't a single server call with an array of records be better than individually calling the updateRecord api? This sounds a lot like premature optimisation to me. Commented Jul 22, 2024 at 6:21

1 Answer 1

2

Instead of calling updateRecord in a for loop, use Promise.all. In the new lwc recipes for lightning-datatable you can find some examples. Look at this one.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.