0

I am creating a lightning component that will have a button that, when pressed, will update the record being viewed and send an email alert. I have managed to get the functionality working as it should be, but I am not sure how to best display error alerts to the user.

As currently designed, my Apex controller returns an Integer value 0, 1, or 2, and based on the number being returned, I would like to display an alert message to the user that states success or failure, and what the failure appears to be.

My problem is that once I pass my return value from my Apex controller function, I am at a loss for what to do with it next.

I first attempted to do a check in the java script controller. I only know how to access the return variable through a callback, so I attempted to set my var to the return value and do the check from there, but was unsuccessful. Code is below-

    var confMessage;
    action.setCallback(this, function(response){
        confMessage = response.getReturnValue();
        component.set("v.retCode", confMessage);
    });
    $A.enqueueAction(action);
    $A.get('e.force:refreshView').fire();

    if(confMessage == 0){
        alert("Success");
    }else{
        alert("Fail");
    }

When I put the variable in the alert itself to see what the js was reading from it, it displayed undefined, so clearly something is amiss there, and I suspect what may be the crux of the issue.

I also attempted to pass it back to the cmp file by setting up an aura attribute named retCode, and was considering setting up an aura:if statement that calls back to the js to display the alert, but as this is my first time doing a button, I cannot figure out how to get the aura to fire the check and call the javascript only after the button is pressed, or how I would do so outside of the init handler or button that can call on click.

I suspect that the issue is my javascript and that I should be able to read the return variable without passing it back to to the component, but some confirmation on this would be great. If this is not the case, an example of how to best pass the code into the lightning component and then using that to call an alert from the JS would be very helpful.

0

Your if/else block needs to be inside your callback function. Right now, your code is firing the action, and immediately moving to the if/else block...so it's firing before you're getting a response from the server. That's why confMessage is undefined.

var confMessage;
action.setCallback(this, function(response){
    confMessage = response.getReturnValue();
    component.set("v.retCode", confMessage);

    if(confMessage == 0){
        alert("Success");
    }else{
        alert("Fail");
    }
});
$A.enqueueAction(action);
$A.get('e.force:refreshView').fire();
  • This was certainly a step in the right direction, I did have to move the variable declaration into the callback method as well, but as soon as I took that step it seemed to work perfectly. My lack of JS exposure continues to be the bane of my existence on this platform, so the assistance is greatly appreciated. – Frank Evers Oct 4 '18 at 21:09
  • No problem! If there must be a bane of your existence, javascript is one you'll share with good company ;) – Shane Steinfeld Oct 4 '18 at 21:15

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.