Display apex error message in lightning component












1















I am trying to display apex catch exception error message in lightning message.
I have tried below in helper. but it didn't work. And let me know how to call this from Lightning component.



Helper:



    saveAction.setCallback(this,function(response){
if (response.getState() == "SUCCESS") {
component.set("v.isShow",false);
var opportunitId = response.getReturnValue();
window.location.href = "/"+opportunitId;
}
});
$A.enqueueAction(saveAction);


Lightning Controller:



    saveRecord : function(component, event, helper)
{
helper.helperSave(component, event);
}









share|improve this question




















  • 1





    You'll need to implement error handling code in your JavaScript controller or handler (depending on where you fire the action). There are examples in the Lightning component developer guide as a guide. There's also a detailed tutorial on the Salesforce Developer blog.

    – David Reed
    Jan 23 at 18:17


















1















I am trying to display apex catch exception error message in lightning message.
I have tried below in helper. but it didn't work. And let me know how to call this from Lightning component.



Helper:



    saveAction.setCallback(this,function(response){
if (response.getState() == "SUCCESS") {
component.set("v.isShow",false);
var opportunitId = response.getReturnValue();
window.location.href = "/"+opportunitId;
}
});
$A.enqueueAction(saveAction);


Lightning Controller:



    saveRecord : function(component, event, helper)
{
helper.helperSave(component, event);
}









share|improve this question




















  • 1





    You'll need to implement error handling code in your JavaScript controller or handler (depending on where you fire the action). There are examples in the Lightning component developer guide as a guide. There's also a detailed tutorial on the Salesforce Developer blog.

    – David Reed
    Jan 23 at 18:17
















1












1








1








I am trying to display apex catch exception error message in lightning message.
I have tried below in helper. but it didn't work. And let me know how to call this from Lightning component.



Helper:



    saveAction.setCallback(this,function(response){
if (response.getState() == "SUCCESS") {
component.set("v.isShow",false);
var opportunitId = response.getReturnValue();
window.location.href = "/"+opportunitId;
}
});
$A.enqueueAction(saveAction);


Lightning Controller:



    saveRecord : function(component, event, helper)
{
helper.helperSave(component, event);
}









share|improve this question
















I am trying to display apex catch exception error message in lightning message.
I have tried below in helper. but it didn't work. And let me know how to call this from Lightning component.



Helper:



    saveAction.setCallback(this,function(response){
if (response.getState() == "SUCCESS") {
component.set("v.isShow",false);
var opportunitId = response.getReturnValue();
window.location.href = "/"+opportunitId;
}
});
$A.enqueueAction(saveAction);


Lightning Controller:



    saveRecord : function(component, event, helper)
{
helper.helperSave(component, event);
}






lightning






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 23 at 18:15









David Reed

35.8k72154




35.8k72154










asked Jan 23 at 18:07









SFDCSFDC

295




295








  • 1





    You'll need to implement error handling code in your JavaScript controller or handler (depending on where you fire the action). There are examples in the Lightning component developer guide as a guide. There's also a detailed tutorial on the Salesforce Developer blog.

    – David Reed
    Jan 23 at 18:17
















  • 1





    You'll need to implement error handling code in your JavaScript controller or handler (depending on where you fire the action). There are examples in the Lightning component developer guide as a guide. There's also a detailed tutorial on the Salesforce Developer blog.

    – David Reed
    Jan 23 at 18:17










1




1





You'll need to implement error handling code in your JavaScript controller or handler (depending on where you fire the action). There are examples in the Lightning component developer guide as a guide. There's also a detailed tutorial on the Salesforce Developer blog.

– David Reed
Jan 23 at 18:17







You'll need to implement error handling code in your JavaScript controller or handler (depending on where you fire the action). There are examples in the Lightning component developer guide as a guide. There's also a detailed tutorial on the Salesforce Developer blog.

– David Reed
Jan 23 at 18:17












1 Answer
1






active

oldest

votes


















5














two things,



In your apex handle the exception like so



try {
// your code here
}
catch (Exception e //if you know what type of error it is going to throw you can be more specific ie. NullPointerException e) {
// "Convert" the exception into an AuraHandledException
throw new AuraHandledException(e.getMessage());
}


Now you have to handle the error in the javascript controller



  saveAction.setCallback(this,function(response){
let state = response.getState();
if (state == "SUCCESS") {
component.set("v.isShow",false);
var opportunitId = response.getReturnValue();
var navEvt = $A.get("e.force:navigateToSObject");
navEvt.setParams({
"recordId": opportunityId
});
navEvt.fire();
} else if(state = "ERROR"){
errorMsg = response.getError()[0];
let toastParams = {
title: "Error",
message: errorMsg, // Default error message
type: "error"
};
let toastEvent = $A.get("e.force:showToast");
toastEvent.setParams(toastParams);
toastEvent.fire();
}
});
$A.enqueueAction(saveAction);
}


Also use the built in object redirect to navigate to the opportunity.



var navEvt = $A.get("e.force:navigateToSObject");
navEvt.setParams({
"recordId": opportunityId
});
navEvt.fire();





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "459"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247721%2fdisplay-apex-error-message-in-lightning-component%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    two things,



    In your apex handle the exception like so



    try {
    // your code here
    }
    catch (Exception e //if you know what type of error it is going to throw you can be more specific ie. NullPointerException e) {
    // "Convert" the exception into an AuraHandledException
    throw new AuraHandledException(e.getMessage());
    }


    Now you have to handle the error in the javascript controller



      saveAction.setCallback(this,function(response){
    let state = response.getState();
    if (state == "SUCCESS") {
    component.set("v.isShow",false);
    var opportunitId = response.getReturnValue();
    var navEvt = $A.get("e.force:navigateToSObject");
    navEvt.setParams({
    "recordId": opportunityId
    });
    navEvt.fire();
    } else if(state = "ERROR"){
    errorMsg = response.getError()[0];
    let toastParams = {
    title: "Error",
    message: errorMsg, // Default error message
    type: "error"
    };
    let toastEvent = $A.get("e.force:showToast");
    toastEvent.setParams(toastParams);
    toastEvent.fire();
    }
    });
    $A.enqueueAction(saveAction);
    }


    Also use the built in object redirect to navigate to the opportunity.



    var navEvt = $A.get("e.force:navigateToSObject");
    navEvt.setParams({
    "recordId": opportunityId
    });
    navEvt.fire();





    share|improve this answer




























      5














      two things,



      In your apex handle the exception like so



      try {
      // your code here
      }
      catch (Exception e //if you know what type of error it is going to throw you can be more specific ie. NullPointerException e) {
      // "Convert" the exception into an AuraHandledException
      throw new AuraHandledException(e.getMessage());
      }


      Now you have to handle the error in the javascript controller



        saveAction.setCallback(this,function(response){
      let state = response.getState();
      if (state == "SUCCESS") {
      component.set("v.isShow",false);
      var opportunitId = response.getReturnValue();
      var navEvt = $A.get("e.force:navigateToSObject");
      navEvt.setParams({
      "recordId": opportunityId
      });
      navEvt.fire();
      } else if(state = "ERROR"){
      errorMsg = response.getError()[0];
      let toastParams = {
      title: "Error",
      message: errorMsg, // Default error message
      type: "error"
      };
      let toastEvent = $A.get("e.force:showToast");
      toastEvent.setParams(toastParams);
      toastEvent.fire();
      }
      });
      $A.enqueueAction(saveAction);
      }


      Also use the built in object redirect to navigate to the opportunity.



      var navEvt = $A.get("e.force:navigateToSObject");
      navEvt.setParams({
      "recordId": opportunityId
      });
      navEvt.fire();





      share|improve this answer


























        5












        5








        5







        two things,



        In your apex handle the exception like so



        try {
        // your code here
        }
        catch (Exception e //if you know what type of error it is going to throw you can be more specific ie. NullPointerException e) {
        // "Convert" the exception into an AuraHandledException
        throw new AuraHandledException(e.getMessage());
        }


        Now you have to handle the error in the javascript controller



          saveAction.setCallback(this,function(response){
        let state = response.getState();
        if (state == "SUCCESS") {
        component.set("v.isShow",false);
        var opportunitId = response.getReturnValue();
        var navEvt = $A.get("e.force:navigateToSObject");
        navEvt.setParams({
        "recordId": opportunityId
        });
        navEvt.fire();
        } else if(state = "ERROR"){
        errorMsg = response.getError()[0];
        let toastParams = {
        title: "Error",
        message: errorMsg, // Default error message
        type: "error"
        };
        let toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams(toastParams);
        toastEvent.fire();
        }
        });
        $A.enqueueAction(saveAction);
        }


        Also use the built in object redirect to navigate to the opportunity.



        var navEvt = $A.get("e.force:navigateToSObject");
        navEvt.setParams({
        "recordId": opportunityId
        });
        navEvt.fire();





        share|improve this answer













        two things,



        In your apex handle the exception like so



        try {
        // your code here
        }
        catch (Exception e //if you know what type of error it is going to throw you can be more specific ie. NullPointerException e) {
        // "Convert" the exception into an AuraHandledException
        throw new AuraHandledException(e.getMessage());
        }


        Now you have to handle the error in the javascript controller



          saveAction.setCallback(this,function(response){
        let state = response.getState();
        if (state == "SUCCESS") {
        component.set("v.isShow",false);
        var opportunitId = response.getReturnValue();
        var navEvt = $A.get("e.force:navigateToSObject");
        navEvt.setParams({
        "recordId": opportunityId
        });
        navEvt.fire();
        } else if(state = "ERROR"){
        errorMsg = response.getError()[0];
        let toastParams = {
        title: "Error",
        message: errorMsg, // Default error message
        type: "error"
        };
        let toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams(toastParams);
        toastEvent.fire();
        }
        });
        $A.enqueueAction(saveAction);
        }


        Also use the built in object redirect to navigate to the opportunity.



        var navEvt = $A.get("e.force:navigateToSObject");
        navEvt.setParams({
        "recordId": opportunityId
        });
        navEvt.fire();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 23 at 18:26









        Calvin OKeefeCalvin OKeefe

        474310




        474310






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Salesforce Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247721%2fdisplay-apex-error-message-in-lightning-component%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Questions related to Moebius Transform of Characteristic Function of the Primes

            List of scandals in India

            Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?