Salesforce Apexメソッドを使用してLWCからデータベースにアクセスする方法

環境
Salesforce Apex

LWCで呼び出すApexメソッドを作成します。
static
publicまたはglobal
@AuraEnabled

サポートされる引数、戻り値は下記になります。
プリミティブ型: Boolean, Date, DateTime, Decimal, Double, Integer, Long, String.
SObject型: 標準オブジェクト、カスタムオブジェクト
Apexクラス(引数に使用する場合、publicなコンストラクタが必要)
コレクション

apexコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public without sharing class LwcTestCtrl {
@AuraEnabled
public static void cancelOrder(Id orderId) {
// 処理コード
}
}
public without sharing class LwcTestCtrl { @AuraEnabled public static void cancelOrder(Id orderId) { // 処理コード } }
public without sharing class LwcTestCtrl {
    @AuraEnabled
    public static void cancelOrder(Id orderId) {
        // 処理コード
    }
}

ApexメソッドをJSファイルにインポートします。
import cancel from '@salesforce/apex/LwcSampleCtrl.cancelOrder’;

インポートされた関数はプロミスを返します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@api recordId;
doCancel() {
this.showSpinner = true;
cancel({ orderId: this.recordId }).then(result => {
const event = new ShowToastEvent({
"title": "注文のキャンセルを確定しました",
"variant": "success"
});
this.dispatchEvent(event);
}).catch(error => {
this.showErrorToast();
}).finally(() => {
this.showSpinner = false;
});
}
@api recordId; doCancel() { this.showSpinner = true; cancel({ orderId: this.recordId }).then(result => { const event = new ShowToastEvent({ "title": "注文のキャンセルを確定しました", "variant": "success" }); this.dispatchEvent(event); }).catch(error => { this.showErrorToast(); }).finally(() => { this.showSpinner = false; }); }
@api recordId;

doCancel() {
    this.showSpinner = true;
    cancel({ orderId: this.recordId }).then(result => {
        const event = new ShowToastEvent({
            "title": "注文のキャンセルを確定しました",
            "variant": "success"
        });
        this.dispatchEvent(event);
    }).catch(error => {
        this.showErrorToast();
    }).finally(() => {
        this.showSpinner = false;
    });
}

 

IT

Posted by arkgame