function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KMK91KMK91 

Delete record in LWC

HI All,

How to delete the record without refresh, becuse record added partially added into data table is not committed into database(in LWC).

Thanks
KMK
Yogesh KanakiYogesh Kanaki
Hey @KMK91 could you please share your code.
Arun Kumar 1141Arun Kumar 1141

Hi,

 

In Lightning Web Components (LWC), you can delete a record without refreshing the entire page by using JavaScript to perform the deletion and then updating the data displayed in your component. If the record has been partially added to a data table but not committed to the database, you can still delete it from your UI and update your data table accordingly. Here's a general outline of how to achieve this:

  • Create a Delete Button: In your LWC component, create a delete button for each record you want to delete.
<template for:each={records} for:item="record">
  <tr key={record.Id}>
    <td>{record.Name}</td>
    <!-- other columns -->
    <td>
      <lightning-button-icon
        icon-name="utility:delete"
        alternative-text="Delete"
        title="Delete"
        data-id={record.Id}
        onclick={handleDelete}
      ></lightning-button-icon>
    </td>
  </tr>
</template>
Implement the Delete Functionality: In your LWC JavaScript file, implement the handleDelete function to delete the record when the delete button is clicked. You can use the lightning-record-edit-form or Apex methods to delete the record.
import { LightningElement, api, wire } from 'lwc';
import { deleteRecord } from 'lightning/uiRecordApi';

export default class MyComponent extends LightningElement {
  @api records;

  handleDelete(event) {
    const recordId = event.currentTarget.dataset.id;

    deleteRecord(recordId)
      .then(() => {
        // Remove the deleted record from the records array
        this.records = this.records.filter(record => record.Id !== recordId);
      })
      .catch(error => {
        // Handle error here
        console.error('Error deleting record: ', error);
      });
  }
}
I Hope this will help you.
Thnaks!
chu jaschu jas
To delete a record without refreshing the page in a Lightning Web Component (LWC), you can use the Lightning Data Service (LDS) or Apex to perform the deletion operation on the client side. This allows you to remove the record from the data table without committing it to the database until explicitly saved by the user. You can also visit the Salesforce program website (https://www.thedailyaquarium.com/aquarium-heater-working/)to learn how to integrate this code.
Brittney PittmanBrittney Pittman
We need to import interface refreshApex for https://dinosaur-game.io refreshing data table once a row is deleted.
Tommy HyattTommy Hyatt
Thanks for the info online games (https://onlgames.io)