TransWikia.com

Handling the errors on the lightning aura component

Salesforce Asked on December 31, 2021

I currently have the aura component makes a call to the server (apex class) and updates the record, I post a toast on the page if any error that happens while updating the record like below

  var action = c.get("c.updateCheck");
    action.setParams({caseId: c.get('v.recordId'), Status: StatusSel});
    action.setCallback(this, result => {
        switch (result.getState()) {
        case "SUCCESS":
        $A.get("e.force:showToast")
        .setParams({
        type: 'success',
        message: 'Case is closed!',
        mode: 'pester'
        })
    .fire();
    break;
    default:
    $A.util.addClass(spinner, "slds-hide");
    var errors = result.getError();
    if (errors) {
        if (errors[0] && errors[0].message) {
            $A.get("e.force:showToast")
            .setParams({
                type: 'error',
                mode: 'pester',
                message: errors[0].message }).fire();  
        }
    }
}
$A.get("e.force:refreshView").fire();

In the above code I get the exact error on the update event like

enter image description here

I am trying to use the lightning data service here instead of the apex class and try to capture the error like below on the onerror action.

handleError: function(component, event, helper) {
    var error = event.getParams();
    var errorMessage = event.getParam("message");
    $A.get("e.force:showToast")
    .setParams({
        type: 'error',
        mode: 'pester',
        message: errorMessage.message }).fire();  
},

But I see the toast like

enter image description here

How can I show the toast as exactly as it shows while using the apex call. Am I missing anything here. Any help is greatly appreciated

One Answer

you should use event.getParam('detail') instead of event.getParams() in my scenario i added validation rule on contact object firstname field.

cmp

   <lightning:recordEditForm
        onerror="{!c.handleError}"
        objectApiName="Contact">
        <lightning:inputField fieldName="FirstName" />
        <lightning:inputField fieldName="LastName" />
        <lightning:inputField fieldName="Email" />

        <div class="slds-m-top_medium">
            <lightning:button variant="brand" class="slds-align_absolute-center" type="submit" name="save" label="Save" />
        </div>
    </lightning:recordEditForm>

controller.js

handleError: function(component,event,helper){
     
    var errordetails = event.getParam('detail');
    console.log("errordetails", JSON.stringify(errordetails));
    var toastEvent = $A.get("e.force:showToast");
    toastEvent.setParams({
        type: 'error',
        mode: 'pester',
        message: JSON.stringify(errordetails)
    });
    toastEvent.fire();
}

enter image description here

Answered by sdandamud1 on December 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP