0

I am having a lightning component that uses the Lightning Data services. The component is like below which has a button which when clicking on it changes the case status to Closed.

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global"  >
<aura:attribute name="loaded" type="Boolean" default="false" />
<lightning:spinner variant="brand" alternativeText="Loading..." class="{!(v.loaded? 'slds-show': 'slds-hide')}" />
 <lightning:recordEditForm aura:id="form" 
        objectApiName="Case" 
        recordId="{!v.recordId}"
        onsubmit="{!c.handleSubmit}"
        onerror="{!c.handleError}"
        onsuccess="{!c.handleSuccess}">
         <!--<lightning:spinner aura:id="spinner" variant="brand" /> -->
        <lightning:inputField aura:id="statusInput" fieldName="Status" class="slds-hide" />
        <lightning:button variant="brand" type="submit" label="Close Case(LDS)" />
 </lightning:recordEditForm>    
</aura:component>

And controller

({
    handleSubmit: function(component, event, helper) {
        component.set('v.loaded',true);
        event.preventDefault();      
        var fields = event.getParam('fields');
        fields.Status = 'Closed';
        console.log(JSON.stringify(fields)); 
        component.find('form').submit(fields); 
        
    },
    
    handleError: function(component, event, helper) {
        component.set('v.loaded',false);
        $A.get("e.force:showToast")
        .setParams({
            type: 'error',
            mode: 'pester',
            message: 'Case cannot be closed, please verify the required fields like Category are filled !!' }).fire();  
    },
    
    handleSuccess: function(component, event, helper) {
        component.set('v.loaded',false);
            $A.get("e.force:showToast")
            .setParams({
            type: 'success',
            message: 'Case is closed!',
            mode: 'pester'
            })
        .fire();
    }
})

I am currently in the handleError hard coding the error message but I am trying to get the errors from the Validation/ if something goes wrong instead of hard coding the errors. I tried to get the error

    var error = event.getParams();
    // Get the error message
    var errorMessage = event.getParam("message"); 

and give it in the message it just gives the generic error message in the toast like enter image description here instead I should be getting the validation error

A Category is required to close the case.

How to get the Validation errors in to the lightning component from the Lightning Data Services.

enter image description here

1

You're on the right track with event.getParams();. The property that hold the error message you're seeking depends on the location of the error defined in your validation rule.

If the error is to appear on a field, the error messages can be found in event.getParam('output').fieldErrors - an object of field errors.

Example:

const fieldErrors = event.getParam('output').fieldErrors;
const separator = ' ';

let message = Object.values(fieldErrors).map(field => {
    return field.map(error => error.message).join(separator);
}).join(separator);

$A.get("e.force:showToast").setParams({
    type: 'error',
    mode: 'pester',
    message: message 
}).fire();

If the error is to appear on the top of the page, the error message can be found in event.getParam('detail') or event.getParam('output').errors - an array of errors.

9
  • I tried using event.getParam('output').errors and event.getParam('detail') but I dont see any error on the toast – user81642 Sep 10 '20 at 19:50
  • Tried all the three options but array is all empty I see no error messages – user81642 Sep 10 '20 at 19:58
  • What do you get in the console when you console.log(event.getParams())? – akiradev Sep 10 '20 at 20:14
  • Proxy {} [[Handler]]: Object [[Target]]: Object [[IsRevoked]]: false – user81642 Sep 10 '20 at 20:17
  • There is no fields in the <lightning:recordEditForm> it just the button, which is when is clicked it tries to update the record status. – user81642 Sep 10 '20 at 20:19
1

onerror is a built-in event of lightning:recordEditForm. So to read the events you must use the event like var err = event.getParam("error");. This returns an object which has

  • message: General description of error.
  • detail: Description of error details, if any.
  • output: Record exception errors with errors and fieldErrors properties. For example, to return the error details when a required field is missing, use event.getParam("output").fieldErrors.
  • error: Returns the status code via status and statusText properties.

Alternatively you can consider using <lightning:messages/> in the component which will automatically display the errors. lightning:messages displays the message, and the detail or fieldErrors record exception message if it's available.

You can find more details here

3
  • Can you please help me how to use the <lightning:messages/> here – user81642 Sep 10 '20 at 19:49
  • Place <lightning:messages/> as a direct child, between your opening <lightning:recordEditForm> and closing </lightning:recordEditForm> tag. However this will show the error message inline rather than as a floating toast. – akiradev Sep 10 '20 at 20:44
  • @akiradev Yes I tried this <lightning:messages/> and dont help with my component – user81642 Sep 10 '20 at 20:53

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.