2

I am trying to implement a custom component to automatically handle errors from the server side actions. I am using getError method to retrieve the information about the errors. As per Aura documentation, the getError() method returns an array of messages. So far I could only achieve an error response with only 1 item in the array returned from that method. I am trying to find a scenario where this method returns more than one error to test my solution properly. Does anyone know how to achieve that?

Here is an extract from the Salesforce documenation that is using getError method.

({
    "echo" : function(cmp) {
        // create a one-time use instance of the serverEcho action
        // in the server-side controller
        var action = cmp.get("c.serverEcho");
        action.setParams({ firstName : cmp.get("v.firstName") });

        // Create a callback that is executed after 
        // the server-side action returns
        action.setCallback(this, function(response) {
            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
            }
            //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");
                }
            }
        });

        // optionally set storable, abortable, background flag here

        // A client-side action could cause multiple events, 
        // which could trigger other events and 
        // other server-side action calls.
        // $A.enqueueAction adds the server-side action to the queue.
        $A.enqueueAction(action);
    }
})
  • 1
    I suggest you think in terms of what's happening server side and the error that gets generated. You may find that approach more helpful. A good example would be DML SaveResults where All or None = false. A controller could return a message for each record in a list that failed to update including the RecordId and the DML error message thrown. – crmprogdev Dec 4 '16 at 17:15
  • 1
    Throwing a DmlException or EmailException can reveal multiple errors, either across multiple records, multiple fields, or both. Otherwise, you'll generally only get one error. – sfdcfox Dec 4 '16 at 18:25
  • Interesting, I thought all but AuraHandledException will result in Unexpected exception message. Tried DML Exception now and it indeed showed multiple rows. However, the number of errors is not equal to the number of processed items and the index attribute inside the errors is null in my experiments which does not allow to trace the error back to the record. – Artur Dec 4 '16 at 21:10
1

As highlighted by sfdcfox, the DMLException and EmailException might procude more than 1 error.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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