0

I have a custom object called Show__c.

This object has a custom field called ShowName__c.

This field is unique/required/case insensitive.

I have a lightning component called ShowClass and its markup is as below

<aura:component controller="ShowClass" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <lightning:input label="Name: " name="LIUI_ShowName" />
    <lightning:input label="Rating: " name="LIUI_ShowRating" />
    <lightning:input label="Genre :" name="LIUI_ShowGenre" /> 
    <lightning:buttonIcon onclick="{!c.fLIUI_Button_Clicked}" iconName="utility:save" variant="bare" size="large" alternativeText="Save" iconClass="dark"/> 
</aura:component>

Its controller is below

({
    fLIUI_Button_Clicked : function(component, event, helper) {
       //console.log("Button clicked");
        //alert("this isa test");
        var action = component.get("c.addShow");
        var testInput = "Breaking Bad";
        action.setParams({showName: testInput });
        // Create a callback that is executed after 
        // the server-side action returns
        action.setCallback(this, function(response) {
            var state = response.getState();
            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 (state === "INCOMPLETE") {
                // do something
                console.log("Incomplete 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);
    }
})

The input payload given to the apex controller is always "Breaking Bad".

Apex Controller

public class ShowClass {

    @AuraEnabled
    public static Id addShow(String showName)
    {
        Show__c s = new Show__c();
        s.ShowName__c = showName;
        insert s;
        return s.Id;

    }

}

I already have a record in Show__c with ShowName__c value as "Breaking Bad" as such any save operation will not work.

This is fine.

What is puzzling why this validation error message is not sent back at all in the callback.

I expected the console log to give me either "Incomplete Error" or "Unknown Error" but there is no log shown in the console at all.

Any clues as to why this is happening ?

1

You get the error object when you write

var errors = response.getError();

if you print this object it will look like this. You can notice here that all require field missing error comes under pageErrors object so if you want to print them you can iterate over on that object and get all the errors

enter image description here

I also noticed that you are using errors[0].message to read the error message. You get this only when you throw custom error message from the apex so if want to you need to modify your code as

public class ShowClass {

    @AuraEnabled
    public static Id addShow(String showName)
    {
        try{

          Show__c s = new Show__c();
          s.ShowName__c = showName;
          insert s;
          return s.Id;
        }
        catch(Exception e){
           throw new AuraHandledException('My Custom error message.');
        }
    }

}

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.