4

Right now, I am aware of the onerror event of the recordForm tag. However, in the controller, event.getParam("errors") doesn't return anything. This is how the controller code looks like:

handleRecordFormErrors : function(component, event, helper) {
    var errorsArr  = event.getParam("errors");
    for (var i = 0; i < errorsArr.length; i++) {
        console.log("error " + i + ": " + JSON.stringify(errorsArr[i]));
    }
}

In the debug logs, a duplicate detection error gets logged, but the error in the frontend is always this: An error occurred while trying to update the record. Please try again., and event.getParams() in the controller gives me nothing. How should I catch this error, or at least change the error message to something user friendly?

  • I see that you're used to Markdown, but unfortunately, the ``` thing doesn't work; you need to use four spaces instead. In the future, consider using the {} button to auto-format your code. – sfdcfox Jun 5 '18 at 5:02
  • it looked ok when i posted. alright noted @sfdcfox – SamuelDev Jun 5 '18 at 5:05
  • 2
    The difference is that the four-spaces generates a proper grey square and is easier to read, especially in large code blocks. It also provides scrolling if it overflows horizontally or is too tall, and has a different effect on mobile devices. – sfdcfox Jun 5 '18 at 5:07
  • Can you put your complete code where you are setting the template. The state should not be SUCCESS in case of error. Please show where the server call happening – Avijit Chakraborty Jun 5 '18 at 5:21
  • Hello @AvijitChakraborty. There is no explicit server call happening because lightning:recordform takes care of that behind the scenes. The loading and the saving of data is all taken cared of by that component. – SamuelDev Jun 5 '18 at 6:58
0

lightning:recordForm is based off of lightning:recordEditForm. If you look at the recordEditForm documentation, you'll see the structure of the params:

EVENT   PARAMETER   TYPE    DESCRIPTION
onerror   error     Object  The error data returned by the form submission.

                            errorCode: An error code with information about the error, 
                                       for example, INSUFFICIENT_PRIVILEGES
                            message: Description of error. We recommend using 
                                     lightning:messages as described in the examples to display your error messages in the UI.

Try using event.getParam("message"); to see the error message, or just console.log(JSON.stringify(event.getParams())); to see the errorCode as well

0

Maybe something like this:

Your.cmp

<aura:component>
    <lightning:notificationsLibrary aura:id="notifLib" /> <!-- this line is really important-->
    <lightning:recordEditForm objectApiName="Account" onload="{!c.handleCreateLoad}" onerror="{!c.handleError}">
    <lightning:messages />
    <lightning:inputField aura:id="nameField" fieldName="Name"/>
    <lightning:button class="slds-m-top_small" type="submit" label="Create new" />
</lightning:recordEditForm>

YourController.js

handleError : function(component, event, helper) {
    var error = event.getParam("error");
    console.log(error);
    component.find("notifLib").showToast({ "variant":"error", "title": "Unexpected Error Here", "message": "A message that you want to show. Can place some error attributes if you need" });
}

Please, don't forget this line in your component, is the key to view the message

<lightning:notificationsLibrary aura:id="notifLib" /> <!-- this line is really important-->

You can check the form and nofitications documentation

Hope to be helpfull! =)

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.