0

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

1

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

  • I dont see anything on the toast when I change from var errorMessage = event.getParam("message"); to detail like var errorMessage = event.getParam('detail'); I see an empty toast and if I stringify the JSON I see ` ]}},"error":]}}},"headers":}}` – user81642 Jul 24 '20 at 17:16
  • what is coming in to console.log("errorMessage ", JSON.stringify(errorMessage )); ? – sdandamud1 Jul 24 '20 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.