Sub topic 14
Limitations and Troubleshooting
This chapter assumes you can build a form with lightning-record-form and lightning-record-edit-form, and that you know the events each one fires. Here you will learn the limits of these components and how to diagnose the problems that come up most often.
Known limitations
The base form components cover common cases. Past a certain point you must switch components or add code.
lightning-record-formgives you little control over layout, field order and validation. Uselightning-record-edit-formwhen you need more.lightning-record-formignoresfieldswhen you setlayout-type, unless you also use thefieldsattribute for extra fields. Check which one you rely on.- Both components work with one object and one record at a time. They do not save related records in a single call.
- Custom logic that runs before save needs
onsubmitonlightning-record-edit-form. - The components respect sharing, object permissions and field-level security. They never show a field the user cannot read.
Diagnosing common problems
A field is missing
Work through these checks in order:
- Confirm the field API name is correct, including the
__csuffix on custom fields. - Check field-level security for the user's profile or permission set. A hidden field is silently left out.
- With
layout-type, confirm the field is on the page layout. Layouts only show fields placed on them. - For a record in edit mode, confirm the field is updateable. Formula and auto-number fields are read-only.
- When you use schema imports, confirm the import path is
@salesforce/schema/Object.Field. A wrong path fails at build time, which makes the error easy to spot.
Validation errors on save
Validation rules, required fields and duplicate rules run on the server. The result depends on the component.
- In
lightning-record-edit-form, addlightning-messagesso field and form errors appear. Without it, the user sees nothing and the form seems to do nothing. - Handle
onerrorto show a toast. Readevent.detail.messagefor the summary. - If you cancel the default submit with
event.preventDefault(), you must callsubmit(fields)yourself. Otherwise nothing saves.
handleError(event) {
// Log the full detail while debugging
console.error(JSON.stringify(event.detail));
}Stale data after a save
Lightning Data Service updates its own cache and the components that use it. Data from Apex does not refresh on its own.
- Components that use the wire service with Lightning Data Service adapters update automatically.
- Components that call Apex through
@wireneedrefreshApex()after a save. - Imperative Apex results need a new call.
import { refreshApex } from '@salesforce/apex';
handleSuccess() {
return refreshApex(this.wiredResult);
}The form does not load
- A
record-idofundefinedmakes the form act as a create form. Wait untilrecordIdis set before you render the form, for example withlwc:if={recordId}. - A missing
object-api-nameis a common cause of an empty form. Set it for every form that is not on a record page.
A quick diagnostic flow
Exercises
Exercise
A user clicks Save on your lightning-record-edit-form and nothing visibly happens. The record is not saved. Name two likely causes and one fix for each.
Show answer
Cause 1: a validation rule or required field fails on the server, and the form has no lightning-messages or onerror handler, so no error shows. Fix: add lightning-messages and an onerror toast. Cause 2: an onsubmit handler calls event.preventDefault() but never calls submit(fields). Fix: call this.template.querySelector('lightning-record-edit-form').submit(fields) inside the handler.
Exercise
A field is in the page layout, but a user does not see it in a lightning-record-form that uses layout-type. What should you check first?
Show answer
Check the user's field-level security for that field, because the form hides fields the user cannot read.