0

In my code I am trying to update Account record. But the update is failed, how do i handle the error in my lightning component.

I tried this in helper:

var state = response.getState();
if(state=="SUCCESS"){

}else if(state=="ERROR"){
  var errors = response.getError();
// setting this error to on of the attribute of the component
}

NOTE: addError method @ field level in apex method cannot be handled in lightning. One of the limitations with respect to handling errors in lightning

3 Answers 3

4

Well, assuming that you have a reference to the component, then you would simply set the error to your attribute like this:

component.set("v.errAttribute", JSON.stringify(errors));

This is assuming that your errors attribute is a simple string, define something like:

<aura:attribute name="errAttribute" type="String" />

You could also extract the message from each error object and put that in an array of strings to render also:

<aura:attribute name="errorMessages" type="String[]" />

Javascript:

var errors = response.getError();
var errorMsgs = errors.map(function(error){
    return error.message;
});
component.set("v.errorMessages",errorMsgs);

Finally, you'd render them using an iteration:

<ul>
    <aura:iteration items="{!v.errorMessages}" var="message">
        <li>{!message}</li>
    </aura:iteration>
</ul>
0
1

Here is how I debug error

Code Snippet

 } else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
1
  • This I Tried but not working.Thank u so much for replying :) Commented Sep 22, 2016 at 7:13
0

First, fantastic that you're considering error handling! In general, when an error occurs due to a direct user interaction (clicking on a button, etc), a message should be displayed to the user. The most common Salesforce pattern is to display error text at the top of the form or next to the component causing the error. A simpler approach that is also common, but doesn't require you to add a bunch of HTML/JS to your code - is to display a toast message.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .