4

I am trying to migrate an Aura component to a Lightning Web Component.

@AuraEnabled public static void updateRecords(Id recordId, String newStatus){...}

I have a public static void apex method which I am calling, but since it is void, the component javascript doesn't handle the response well.

submitResponse(){
    this.showSpinner = true;
    updateRecords({recordId: this.recordId, newStatus: this.newStatus})
    .then(result => {
        console.log("result");
        console.log(result);
        if (result) { //never true
            this.showToast("Success!", "Success", "error");
            this.showSpinner = false;
        }
        this.showSpinner = false;
    })
    .catch(error => {
        this.showToast("Error!", "error", "error");
        console.log(error);
        this.error = error;
        this.showSpinner = false;
    });

}

What are the best practices for using void return type on Apex methods which are called from a lightning web component?

5

Unlike LC (Aura), the parameters sent back are different. If you reach the ".then", the call was successful, otherwise there was an error. There's no need for you to check if the result was successful.

submitResponse(){
    this.showSpinner = true;
    updateRecords({recordId: this.recordId, newStatus: this.newStatus})
    .then(() => {
        this.showToast("Success!", "Success", "No error!");
        this.showSpinner = false;
    })
    .catch(error => {
        this.showToast("Error!", "error", "error");
        this.error = error;
        this.showSpinner = false;
    });
}
  • Thanks sfdcFox! works like a charm :) – Mitch Spano 2 days ago

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.