deleteRecord(recordId)

レコードを削除します。

構文 

1import { deleteRecord } from 'lightning/uiRecordApi';
2deleteRecord(recordId: string): Promise<void>

ユーザインターフェース API リソース 

1DELETE /ui-api/records/{recordId}

deleteRecord は、このユーザインターフェース API リソースを使用します。

パラメータ 

  • recordId — (必須) 削除するレコードの ID。

戻り値 

戻り値の型は void です。

使用方法 

deleteRecord を使用するには、いくつかの方法があります。たとえば、レコードのリストを表示し、リストの各レコードに対して削除を行うことができます。lwc-recipes GitHub リポジトリの ldsDeleteRecord コンポーネントを参照してください。

この例では、レコードページに削除ボタンを追加しています。レコードを削除すると、lightning/platformShowToastEvent モジュールを使用して、トーストメッセージが表示されます。レコードが削除されると、ユーザは、lightning/navigation を使用してレコードホームページにリダイレクトされます。

1import { LightningElement, api, track } from "lwc";
2import { deleteRecord } from "lightning/uiRecordApi";
3import { ShowToastEvent } from "lightning/platformShowToastEvent";
4import { NavigationMixin } from "lightning/navigation";
5
6export default class DeleteExample extends NavigationMixin(LightningElement) {
7  // Flexipage provides recordId and objectApiName
8  @api recordId;
9  @api objectApiName;
10
11  @track error;
12  delete(event) {
13    deleteRecord(this.recordId)
14      .then(() => {
15        this.dispatchEvent(
16          new ShowToastEvent({
17            title: "Success",
18            message: "Record deleted",
19            variant: "success",
20          }),
21        );
22        // Navigate to a record home page after
23        // the record is deleted, such as to the
24        // contact home page
25        this[NavigationMixin.Navigate]({
26          type: "standard__objectPage",
27          attributes: {
28            objectApiName: "Contact",
29            actionName: "home",
30          },
31        });
32      })
33      .catch((error) => {
34        this.dispatchEvent(
35          new ShowToastEvent({
36            title: "Error deleting record",
37            message: error.body.message,
38            variant: "error",
39          }),
40        );
41      });
42  }
43}

このボタンは、delete() メソッドをコールします。

1<template>
2  <lightning-button
3    variant="destructive"
4    label="Delete"
5    icon-name="utility:delete"
6    icon-position="right"
7    onclick={delete}
8  >
9  </lightning-button>
10</template>

The Japanese Summer '24 guide is now live

日本語の Summer '24 ガイドが公開されました! 「Component Reference (コンポーネントリファレンス)」は、以前と同様にコンポーネントライブラリにあります。