0

Hi i want the error message to be displayed, i am getting value in response.getError() function, i want message argument in this function. Below is the snippet of how i'm doing, i am getting undefined in str console statement.

JS

  var errors = response.getError();
           if (errors) {
  var str = errors[0].message;
  console.log('str' + JSON.stringify(str));
  component.set("v.Error", str);
  console.log('Here cmp' + component.get("v.Error")); }
6
  • What if you console.log errors ? Apr 17, 2017 at 12:42
  • @FabienTaillon Hi sorry i did not get you.
    – Hamza
    Apr 17, 2017 at 12:52
  • Sorry for being unclear, can you do a console.log(errors) ? Apr 17, 2017 at 12:55
  • @FabienTaillon I'm getting the value there, by doing JSON.stringify i'm getting clear error that includes pageerrors, message.
    – Hamza
    Apr 17, 2017 at 13:02
  • [{"fieldErrors":{},"pageErrors":[{"statusCode":"FIELD_CUSTOM_VALIDATION_EXCEPTION","message":"There is already an opportunity with the same account name"}],"index":null,"duplicateResults":[]}]
    – Hamza
    Apr 17, 2017 at 13:05

2 Answers 2

0

Did you try fetching the error as specified in the documentation for Calling a Server Side Actions here.

In your callback:

var state = response.getState();
        // This callback doesn’t reference cmp. If it did,
        // you should run an isValid() check
        //if (cmp.isValid() && state === "SUCCESS") {
        if (state === "SUCCESS") {
            // Alert the user with the value returned 
            // from the server
            alert("From server: " + response.getReturnValue());

            // You would typically fire a event here to trigger 
            // client-side notification that the server-side 
            // action is complete
        }
        //else if (cmp.isValid() && state === "INCOMPLETE") {
        else if (state === "INCOMPLETE") {
            // do something
        }

This is the part that interests you, validate that the state is in Error and fetch the response error message:

        else if (cmp.isValid() && state === "ERROR") {
        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");
            }
0

I would also take a look at this answer - in my experience, you have to iterate through various levels of the error object to actually get the details you want e.g.:

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;
                                }
                            }
                        }
                    }

You must log in to answer this question.

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