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
Andrew Aldis 15Andrew Aldis 15 

Scroll to the top of the page if there is an error

I created a lighting component to create a record using the <lightning:recordEditForm> component and a standard submit button.  It works well but it displays any error messages on the top of the screen.  I am looking for a way to check if there is an error message and if there is I would like to scroll back to the top of the page for review.  Does anyone have any ideas?
Alain CabonAlain Cabon
Hi,

Did you use <lightning:messages />​ ?
https://developer.salesforce.com/docs/component-library/bundle/lightning:recordEditForm/documentation

When there is an error, the event onerror is triggered but the scrolling with some JS code is not standard in the lex components.

The standard alternative could be a simple toast using the errorCode and message of the event onerror. 

Event: 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.


onerror="{!c.handleError}"

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_validate_fields.htm
handleError: function (component, event, helper) {
    var params = event.getParams(); 
     
    var toast = $A.get("e.force:showToast");
    if (toast){
        //fire the toast event in Salesforce app and Lightning Experience
        toast.setParams({
            "title": "Error!",
            "message": "Error Code:" + params.errorCode + "\nMessage:" + params.message
        });
        toast.fire();
    } 
}
SalesForce_52436SalesForce_52436
Hi Andrew,

Did you get any answer? I am looking for same problem too..