1

Does anyone have a good example of how to capture server side errors when I am creating new records? I have been searching and am struggling to incorporate a message to the user if a duplicate record exists or to display validation rules.

Here is my save function:

 SaveClones : function(component, ClonedGroupStructures) {
        //call apex to save the newly created group structures
    var action = component.get("c.saveGroupStructure");
        action.setParams({gs: ClonedGroupStructures });

     //Here we set the callback for the request - this is what happens when the server responds.
    action.setCallback(this, function(response){
    var state = response.getState();
    if (state === "SUCCESS") {
        //here you get the expenses attribute that is from the compenent
        console.log('### record saved');
        this.loadGroupStructures(component);
        component.set("v.cloning", "false");
        this.handleSelectChange(component);
    }
    else if(state === "ERROR") {

       toastEvent.setParams({
                    "title": "Error!",
                    "message": " Something has gone wrong."
            });
        toastEvent.fire();
    }
    }) 

$A.enqueueAction(action);

}

Here is my Method to Save

public static List<Group_Structure__c> saveGroupStructure (List<Group_Structure__c> gs) {
List<Group_Structure__c> insertGS = new List<Group_Structure__c>(); 
    //I use a deepclone to get rid of the ID of the original records
   // insertGS = gs.deepClone();

system.debug ('### IM the saveGroupStructures - here is the gs ' + gs);
    try{
     insert gs;
    return gs;
    }
    catch(Exception ex) {
        throw new AuraHandledException('The records did not save: '
                                        + ex.getMessage());
    }
    // return '';    
}
  • Thats the thing. I'm confused on how to put it together. I've added a try catch to my apex but struggling on how to get that to my component. – Fred Jan 8 at 16:35
  • Can you add that? Also I cant answer without knowing the context, but this is good starting point developer.salesforce.com/docs/atlas.en-us.lightning.meta/… – Pranay Jaiswal Jan 8 at 16:40
  • Thanks Pranay.. I updated the original post to show what I have for the Save function and the apex method.. thanks!! Fred – Fred Jan 8 at 17:16
0

Whenever an error occurs, you can get the errors using response.getError(), it also contains the error message that you can use in your toast message.

else if(state === "ERROR") {
        var errors = response.getError();
        if (errors) {
            if (errors[0] && errors[0].message) {
                toastEvent.setParams({
                            "title": "Error!",
                            "message": errors[0].message,
                            "mode" : "sticky"
                    });
                toastEvent.fire();
            }
        }
}

Src: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm

  • Thank you! That definitely helped! The toast pops up and then disappears. Is that best practice in this case? I see the user possibly missing it since it goes away? – Fred Jan 8 at 18:16
  • There are various ways to handle it. One way would be to create an errorDiv and show error message buy setting component attribute. Second would be to make your toast not dismiss and stick. You can do it via adding a parameter in your toast "mode" : "sticky" src :developer.salesforce.com/docs/component-library/bundle/… – Pranay Jaiswal Jan 8 at 18:19
  • That's perfect! Last question. I assume that I should also build checks on the component/function to check the fields for things like, right type, not null, etc. is that correct? thanks again!!!!! – Fred Jan 8 at 18:22
  • There is no harm in having extra checks.. Now a days the devices are quite powerful so the enduser wont even feel the lag – Pranay Jaiswal Jan 8 at 18:24
  • Awesome, thanks again Pranay! I really appreciate your help in clarifying for me!!! – Fred Jan 8 at 18:31

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.