function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aratz GuerraAratz 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:
 
<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?
 
Best Answer chosen by Aratz Guerra
Aratz GuerraAratz Guerra
Hi, finally I used this:
 
else if (saveResult.state === "ERROR") {
                    var errors = "";
                    for (var i = 0; saveResult.error.length > i; i++){
                        errors = errors + saveResult.error[i].message;
                    }            
                    var resultsToast = $A.get("e.force:showToast");
                    resultsToast.setParams({
                        "type":"error",
                        "title": "Error!",
                        "message": errors                        
                    });
                    resultsToast.fire();
                }

Thank you for the help Ghanshyam Choudhari :)

All Answers

GhanshyamChoudhariGhanshyamChoudhari
Hi Aratz,
please  try below code
 
else if (state === "ERROR") {
                    var errors = response.getError();
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        "type":"error",
                        "title": "Error!",
                        "message": errors
                    });
                    toastEvent.fire();
                }

 
Aratz GuerraAratz Guerra
Hi, finally I used this:
 
else if (saveResult.state === "ERROR") {
                    var errors = "";
                    for (var i = 0; saveResult.error.length > i; i++){
                        errors = errors + saveResult.error[i].message;
                    }            
                    var resultsToast = $A.get("e.force:showToast");
                    resultsToast.setParams({
                        "type":"error",
                        "title": "Error!",
                        "message": errors                        
                    });
                    resultsToast.fire();
                }

Thank you for the help Ghanshyam Choudhari :)
This was selected as the best answer
The AshokAThe AshokA
helpful