5

I'm getting this error on trying to submit the form that I created on my custom lightning component but I can't find a way to see more details about the error.

How would I know what causing the error ??

I tried these onerror functions:

getError : function(component, event, helper){
    console.log("@@@@omeememmee:   " + event.getParam("error"));
}
getError : function(component, event, helper){
    console.log("@@@@omeememmee:   " + error.getParam("error").errorCode);
}

getError : function(component, event, helper){
    console.log("@@@@omeememmee:   " + error.message);
}

getError : function(component, event, helper){
    console.log("@@@@omeememmee:   " + error.errorCode);
}

All returns undefined.

1
  • I forgot to mention that it's a form for a record creation. Not update.
    – osos9
    Aug 22, 2018 at 9:02

2 Answers 2

6

The documentation seems to be a bit misleading, at best. Having just played around with it, the object you get back can have lots of details in it:

({
    getError: function(component, event, helper) {
        var error = event.getParam("error");
        console.log(error.message); // main error message

        // top level error messages
        error.data.output.errors.forEach(
            function(msg) { console.log(msg.errorCode); 
                           console.log(msg.message); }
        );

        // field specific error messages
        Object.keys(error.data.output.fieldErrors).forEach(
            function(field) { 
                error.data.output.fieldErrors[field].forEach(
                    function(msg) { console.log(msg.fieldName); 
                                   console.log(msg.errorCode); 
                                   console.log(msg.message); }
                )
            });
    }
})

If you're interested in learning more, I suggest you JSON.stringify the error parameter to see what else it contains.

4
  • 1
    JSON.stringify is really your friend for this one in my experience, along with creating deliberate errors so you can see what actually appears when you have an an error e.g. create a validation rule that you know will fail, so you can see where it appears in the object structure Aug 22, 2018 at 13:18
  • Dec 2018 update - if you end up here, something seems to have changed in the error object (it doesn't seem to have the error.data nodes anymore) So I would suggest you do the stringify method to confirm the object structure before trying to parse it Dec 4, 2018 at 22:07
  • 1
    @BritishBoyinDC That's a good point. I don't think the docs explicitly specified what the format was/is, just that it exists. This might be a thing you'd want to ask the documentation team or whoever handles those specific docs.
    – sfdcfox
    Dec 4, 2018 at 22:26
  • The other thing that concerns me is that this doesn't seem to be versioned in any way...this code was fine before Winter 19, and I didn't update the versions...and then it broke Dec 5, 2018 at 0:40
0

My take away in trying to parse the errrors is that there are different nodes in the error object - e.g. one for field validation errors, another one for general errors like Duplicate Detection, so if you need to display different messages depending on the error in something like a Toast, it helps to try and break it down:

 var message = '';

        var errors = event.getParams();
        var errormessages = errors.error.data.output;
                    if ($A.util.isEmpty(errormessages.fieldErrors) === false) {
                        if (errormessages.fieldErrors.Name.length > 0) {
                            for (var j = 0; errormessages.fieldErrors.Name.length > j; j++) {
                                var fielderror = errormessages.fieldErrors.Name[j];
                                message += fielderror.errorCode + ' (' + fielderror.field + ') : ' + fielderror.message;
                            }
                        }
                    }

                    if ($A.util.isEmpty(errormessages.errors) === false) {
                        if (errormessages.errors.length > 0) {
                            for (var j = 0; errormessages.errors.length > j; j++) {
                                var generalerror = errormessages.errors[j];
                                if (generalerror.errorCode === 'DUPLICATES_DETECTED') {
                                    message += 'Looks like this might be a duplicate. Click on the “Return to Search” button to find this contact';
                                }
                                else {
                                    message += generalerror.errorCode + ' (' + generalerror.field + ') : ' + generalerror.message;
                                }
                            }
                        }
                    }
1
  • Also, if anyone is looking at this for help with the newer recordForm, I found that the error logs don't work at all right now due to a KI so don't drive yourself crazy wondering why this approach doesn't work Aug 22, 2018 at 13:16

You must log in to answer this question.

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