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
Rahul Jain 199Rahul Jain 199 

Display a Message Error on Lightning Component From Apex Server Side.

Hello,

I'm trying to display a message error on the lightning component when I get some error on my apex class, like when I call apex and delete account and Customeobject__c but one of them is not deleted and  response will be ''Success"

Please, can anyone could help me?
Best Answer chosen by Rahul Jain 199
Maharajan CMaharajan C
Hi Rahul,

You have to use the AuraHandledException to throw the Error from Apex Class to Lightning Component.

Use the AuraHandledException in catch block of Apex Class to throw the Error. Like Below

try{
      delete Accounts;
}
catch(Exception e){
       throw new AuraHandledException(e.getMessage());
 }



Now in JS callback the below error code block will execute:

else if(state === "ERROR"){
                var errors = action.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        alert(errors[0].message);
                    }
                }



Reference Links:
https://developer.salesforce.com/blogs/2017/09/error-handling-best-practices-lightning-apex.html
https://www.biswajeetsamal.com/blog/apex-controller-exception-handling-in-lightning-component/
https://webkul.com/blog/exception-handling-lightning-component/

Thanks,
Maharajan.C