trailheadapps / lwc-recipes

A collection of easy-to-digest code examples for Lightning Web Components on Salesforce Platform
https://developer.salesforce.com
Creative Commons Zero v1.0 Universal
2.59k stars 3.07k forks source link

Add support for validation rule error messages to the reduceErrors function #557

Closed HerbertHBT closed 3 years ago

HerbertHBT commented 3 years ago

What is missing from the application? Please describe. Just reposting a request that was closed without response. The Error Handling Best Practices for Lightning Web Components article on the Salesforce Developers Blog recommends using reduceErrors to handle different kinds of error objects. However, this function doesn't elegantly handle validation rules. For instance, when updating a record, it returns "An error occurred while trying to update the record. Please try again." instead of the text defined in the validation rule. I've attached an image from Chrome Debugger showing that the validation rule text is available in the error data structure.

Describe the solution you'd like It would be very helpful if the error defined for the validation rule was included in the result of the reduceErrors() function.

Additional context

validationRule
github-actions[bot] commented 3 years ago

Welcome! 👋

Thank you for posting this issue. 🙇🏼‍♂️ We will come back to you latest within the next 48h (working days). Stay tuned!

HerbertHBT commented 3 years ago

Hi @adityanaag3,

Amazing work! Thanks very much. I saw the update and it looks amazing. But I think it will work only when the validation rule is set to show the error on the field. If the validation is set to show the error o the top of the page, the fieldErrors will be empty, but the error.body.output.errors will be populated.

Thanks again and have a great weekend.

adityanaag3 commented 3 years ago

Hey @HerbertHBT There is a PR #559 I've been working on that addresses this issue. Another PR from a different contributor, fixed this issue partially, so I linked it, and merging that seems to have closed this issue - Reopening until 559 is merged.

sivasank4sfdc commented 10 months ago

Hi @adityanaag3

Please correct me if I'm wrong, I used the lightning-record-edit-form onerror={handleError} and observed that the error object format has changed, received the below format. { "message": "An error occurred while trying to update the record. Please try again.", "detail": "", "output": { "errors": [], "fieldErrors": { "Province__c": [ { "constituentField": null, "duplicateRecordError": null, "errorCode": "FIELD_CUSTOM_VALIDATION_EXCEPTION", "field": "Province__c", "fieldLabel": "Province", "message": "Province is Mandatory when state is blank" } ] } } } And the output show "message": "An error occurred while trying to update the record. Please try again."

adityanaag3 commented 10 months ago

Hey @sivasank4sfdc When using lightning-record-edit-form, you needn't manually handle and display errors. If you include the <lightning-messages></lightning-messages> tag, it will show the correct message in the UI.

reduceErrors works best when directly using adapters from lightning/uiRecordApi.

sivasank4sfdc commented 10 months ago

Hey @sivasank4sfdc When using lightning-record-edit-form, you needn't manually handle and display errors. If you include the <lightning-messages></lightning-messages> tag, it will show the correct message in the UI.

reduceErrors works best when directly using adapters from lightning/uiRecordApi.

Thank you adityanaag3