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
    5 hours ago


















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
    5 hours ago
















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 5 hours ago









David Reed

32.6k72052




32.6k72052










asked 5 hours ago









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
    5 hours ago
















  • 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
    5 hours ago










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
5 hours ago







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
5 hours ago












1 Answer
1






active

oldest

votes


















4














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









    4














    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




























      4














      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


























        4












        4








        4







        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 4 hours ago









        Calvin OKeefeCalvin OKeefe

        389210




        389210






























            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

            Histoire des bourses de valeurs

            Why is there Russian traffic in my log files?

            Rename multiple files to decrement number in file name?