Sub topic 14
Limitations and Troubleshooting
This chapter assumes you have built forms with lightning-record-form and lightning-record-edit-form. You should know how @api recordId, event handlers and @salesforce/schema imports work. Here you will learn what the form components cannot do, and how to diagnose the problems you will meet most often.
How the form components fail
Both form components are built on Lightning Data Service (LDS). LDS enforces sharing rules, object permissions and field-level security (FLS) for the running user. Most problems fall into one of four groups:
- The user lacks access, so a field or record does not appear.
- The component is configured in a way it does not support.
- The server rejects the save, for example because of validation rules.
- The save worked, but other parts of the page still show old data.
Work out which group you are in before changing code. The diagram shows a quick way to sort a problem.
Limitations of lightning-record-form
lightning-record-form trades control for speed. These limits are by design.
- No custom layout. You get either the
fieldsyou list or a page layout throughlayout-type. You cannot reorder, group or style individual fields, and you cannot place other markup between fields. - Limited validation hooks. You can react to
onsubmit, but you have no per-field access in the template. If you need to validate one field against another,lightning-record-edit-formis a better fit. fieldsandlayout-typeinteract. If you set both, the form uses the fields fromfieldsand the layout fields. Setting neither leaves the form with nothing to show. Choose one source on purpose.- Fixed field types. Each field renders with the control its data type implies. You cannot swap in a custom input for one field.
- Not every field type works. Some fields, such as certain compound or system fields, are not editable or are not supported by the form components. Check the reference documentation for the object you use.
layout-type="Full"includes everything on the page layout. This may show more fields than you expect. UseCompactor an explicitfieldslist when you want a smaller form.
Limitations of lightning-record-edit-form
lightning-record-edit-form gives control, but it also adds work.
- It needs explicit children. Without
lightning-input-fieldorlightning-output-fieldchildren, the form shows nothing. A form with only a submit button is valid markup and does nothing useful. - It has no built-in error display. Add
lightning-messagesto show server errors. If you leave it out, a failed save can look like nothing happened unless you handleonerror. - Only the supported children work.
lightning-input-fieldmust be a direct descendant of the form in the template. Wrapping it in a child component breaks the connection to the form, because the child component renders in its own shadow tree. - The submit button must be inside the form. A
lightning-buttonwithtype="submit"outside the form tags does not submit it. - No input for unsupported field types.
lightning-input-fieldsupports many but not all data types. Check the documentation if a field renders as blank.
Common problems and their causes
| Symptom | Likely cause | What to check |
|---|---|---|
| A field is missing from the form | The user has no FLS read access, or the field name is wrong | Profile or permission set, spelling of the API name |
| A field shows but cannot be edited | No edit access, a formula field, or a read-only system field | FLS edit permission, field type |
| The form shows no data | `recordId` is undefined when the form first renders | Where `recordId` comes from, and whether the component is on a record page |
| Console error about an invalid object | `object-api-name` is wrong or the object is not available to the user | Spelling, namespace prefix, object permissions |
| Save fails with a message | A validation rule, a required field, or a duplicate rule | Setup, validation rules, required fields in the layout |
| Save works but another component shows old data | That component caches its data and does not know about the change | Refresh strategy |
| Picklist is empty or shows wrong values | Record type is not set or not available to the user | `record-type-id`, record type access |
| Handler does not run | Event name is misspelled in the template | `onsuccess`, `onerror`, `onsubmit` in lowercase |
Fields that do not appear
Missing fields are the most frequent question. Check these in order.
- Spelling. Field API names are case-sensitive in practice and custom fields end in
__c. Prefer@salesforce/schemaimports, because a wrong import fails at build or deploy time instead of silently showing nothing. - Field-level security. The running user needs at least read access. Test as the actual user, not as an administrator.
- Page layout. With
layout-type, only fields on that layout appear. A field that is not on the layout is not shown, even if the user can read it. - Record type. The layout and the picklist values can vary by record type. Pass
record-type-idwhen you need a specific one. - Mode. In
readonlymode, or for fields the user cannot edit, you see output instead of an input.
A schema import also guards against a different mistake. This component imports fields and uses them in fields:
import { LightningElement, api } from 'lwc';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountForm extends LightningElement {
@api recordId;
fields = [NAME_FIELD, INDUSTRY_FIELD];
}If Account.Industry is misspelled in the import path, the deployment reports the problem. If you had written the string 'Industri', you would only find out at run time.
Save errors
Errors can come from the server or from the client.
- Validation rules and required fields return a message from the server.
lightning-messagesshows it inside the form.onerrorgives youevent.detailso you can show a toast or log it. - Field-level errors appear next to the field when the input knows which field failed.
- Page-level errors appear at the top of the form, through
lightning-messages. - Client-side checks run first. A required input left empty blocks the submit before any request is sent, so
onerrordoes not fire for it.
When you override submit, remember that event.preventDefault() cancels the default save. If you then forget to call submit(fields), nothing is saved and nothing reports an error. This is the most common reason for a "silent" form.
handleSubmit(event) {
event.preventDefault();
const fields = event.detail.fields;
fields.LastName = (fields.LastName || '').trim();
this.template
.querySelector('lightning-record-edit-form')
.submit(fields);
}Stale data after a save
The form updates the record through LDS, and LDS refreshes the components that use the same record through LDS wire adapters. Components that fetched the data some other way do not know about the change. This includes data loaded by an imperative Apex call, or by a wire to an Apex method.
- Wire adapters from
lightning/uiRecordApiupdate automatically. - Apex-backed data needs
refreshApexon the wired result, or a new imperative call. - A parent that passes values down as properties needs to hear about the save. Dispatch a custom event from your
onsuccesshandler so the parent can reload.
Do not assume that a toast means the whole page is current. A toast only reports that your handler ran.
Debugging approach
Follow a short routine when a form misbehaves.
- Open the browser developer console and read the first error, not the last.
- Confirm the inputs: log
recordId,objectApiNameand thefieldsarray. - Attach
onerrorand logevent.detail. It often names the exact field or rule. - Reproduce as the affected user. Use "Login as" in a sandbox, or check the permission sets assigned.
- Reduce the form to two fields. If the small form works, add fields back until the problem returns.
- Check Setup for validation rules, required fields, duplicate rules, and flows or triggers on the object. These run on every save from the form.
Choosing a different tool
Sometimes the right fix is to leave the form components. Consider other options when you need:
- Cross-object saves in one action, or complex business logic. Use Apex.
- Full control over rendering and validation of each input. Use
lightning-inputwithcreateRecordorupdateRecordfromlightning/uiRecordApi. - Read-only display with a custom layout. Use
lightning-record-view-form.
These cost more code, so move only when the form components cannot meet the requirement.
Exercise
A developer wraps a lightning-input-field in a child component and places that child inside lightning-record-edit-form. The field appears but its value is never saved. Why, and what should they do?
Show answer
The child component renders in its own shadow tree, so the input field is not a direct part of the form's template and the form does not collect its value. Place the lightning-input-field elements directly inside the lightning-record-edit-form in the same template.
Exercise
An onsubmit handler calls event.preventDefault() and checks that a field is not empty. When the field is empty, the handler returns. Users report that clicking Save does nothing. What is missing?
Show answer
The handler gives no feedback on the failed check, and in the passing case it must call submit(fields) on the form. Show a message when the check fails, and call this.template.querySelector('lightning-record-edit-form').submit(fields) when it passes.
Exercise
A form saves a Contact, and the toast says success, but a related list component built on an Apex wire still shows the old data. What should you do?
Show answer
The Apex-backed component is not refreshed by LDS. Call refreshApex on its wired result after the save, or dispatch a custom event from onsuccess so the parent reloads the data.