You need to sign in to do that
Don't have an account?
Aratz Guerra
Capture and show error in lightning - saveRecord
Hi, I need to capture and show in the page any error that could happens. I have this component:
and the controller:
Could anyone help me please?
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" access="global"> <!-- Define Attribute--> <aura:attribute name="newQuote" type="Object"/> <aura:attribute name="simpleNewQuote" type="Object"/> <aura:attribute name="newQuoteError" type="String"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <force:recordData aura:id="QuoteRecordCreator" layoutType="FULL" targetRecord="{!v.newQuote}" targetFields="{!v.simpleNewQuote}" targetError="{!v.newQuoteError}" /> </aura:component>
and the controller:
handleSaveQuote: function(component, event, helper) { component.set("v.simpleNewQuote.SBQQ__Opportunity2__c", component.get("v.recordId")); component.set("v.simpleNewQuote.SBQQ__LineItemsGrouped__c", true); component.set("v.simpleNewQuote.SBQQ__Primary__c", true); component.find("QuoteRecordCreator").saveRecord(function(saveResult) { if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") { // record is saved successfully var resultsToast = $A.get("e.force:showToast"); resultsToast.setParams({ "type": 'success', "title": "Saved", "message": "The new quote has been created." }); resultsToast.fire(); helper.navigateTo(component, saveResult.recordId); } else if (saveResult.state === "INCOMPLETE") { // handle the incomplete state console.log("User is offline, device doesn't support drafts."); } else if (saveResult.state === "ERROR") { // handle the error state var resultsToast = $A.get("e.force:showToast"); resultsToast.setParams({ "type": 'error', "title": "Not Saved", "message": "The new quote has an error." }); resultsToast.fire(); helper.navigateTo(component, saveResult.recordId); console.log('Problem saving contact, error: ' + JSON.stringify(saveResult.error)); } else { console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error)); } }); }if saveResult.state === "ERROR" I need to show a message (like in the SUCCESS case) but it can be generic message, I must capture the error.
Could anyone help me please?
Thank you for the help Ghanshyam Choudhari :)
All Answers
please try below code
Thank you for the help Ghanshyam Choudhari :)