Display apex error message in lightning component
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
add a comment |
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
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
add a comment |
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
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
lightning
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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();
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
add a comment |
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();
add a comment |
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();
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();
answered 4 hours ago
Calvin OKeefeCalvin OKeefe
389210
389210
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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