Sub topic 6
Navigating to record and object pages
This chapter covers the two page types you will use most often with NavigationMixin.Navigate: standard__recordPage and standard__objectPage. It assumes you know the basic LWC file structure, @api properties, and how a PageReference is shaped. Earlier chapters covered applying the mixin and calling Navigate. Here the focus is on the attributes each page type needs and the mistakes that come up most often.
The two page types at a glance
A PageReference is a plain object with a type, an attributes object, and an optional state object. The type tells Lightning Experience which kind of page to open. The attributes identify the target.
| Page type | Opens | Required attributes |
|---|---|---|
| `standard__recordPage` | A single record | `recordId`, `actionName` |
| `standard__objectPage` | An object's home, list, or New form | `objectApiName`, `actionName` |
Navigating to a record page
A record page needs the record's ID and an action name. The actionName value is one of view, edit, or clone.
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class OpenRecord extends NavigationMixin(LightningElement) {
@api recordId;
handleView() {
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: this.recordId,
objectApiName: 'Account',
actionName: 'view'
}
});
}
}The recordId is the 15- or 18-character ID of the record. The objectApiName is optional in many cases because Salesforce can work out the object from the ID. Include it when you know it. It avoids an extra lookup and is required in some contexts, such as Experience Cloud sites.
Choosing an action
viewopens the record detail page.editopens the record in edit mode.cloneopens the clone form, pre-filled from the source record.
The edit and clone actions are not supported everywhere. In some containers, such as the mobile app or certain Experience Cloud templates, they may behave differently. If an edit action does not open as expected, navigate to view and let the user choose Edit from the page.
Navigating to an object page
An object page needs the API name of the object and an action name. The supported action names are home, list, and new.
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class OpenObject extends NavigationMixin(LightningElement) {
handleNewContact() {
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Contact',
actionName: 'new'
}
});
}
handleAccountHome() {
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Account',
actionName: 'home'
}
});
}
}| `actionName` | Result |
|---|---|
| `home` | The object's home page |
| `list` | A list view of the object. Requires a `filterName` in `state` |
| `new` | The New record form |
Custom objects
For a custom object, use the full API name, including the __c suffix. If the object belongs to a managed package, include the namespace prefix as well.
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Invoice__c',
actionName: 'home'
}
});Opening a list view
The list action needs to know which list view to show. You pass the list view in state.filterName. Use either the list view's API name or its ID. Standard list views also have well-known names such as Recent.
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class OpenRecentContacts extends NavigationMixin(LightningElement) {
handleList() {
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Contact',
actionName: 'list'
},
state: {
filterName: 'Recent'
}
});
}
}The state object holds page parameters. In Lightning Experience, each key in state must be prefixed with a namespace and two underscores (for example c__myParam) when it is a custom parameter. The standard filterName key is the exception.
Pre-filling a new record form
When you open the New form, you can set default field values with defaultFieldValues. This property goes in state. Its value is a string of comma-separated Field=value pairs. Encode the string with encodeDefaultFieldValues from lightning/pageReferenceUtils.
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
export default class NewContactForAccount extends NavigationMixin(LightningElement) {
@api recordId;
handleNewContact() {
const defaults = encodeDefaultFieldValues({
AccountId: this.recordId,
LastName: 'Smith'
});
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Contact',
actionName: 'new'
},
state: {
defaultFieldValues: defaults
}
});
}
}Using the helper means you do not have to escape commas or special characters by hand.
How navigation runs
Navigate does not return a value you wait on. It asks the framework to change the page, and the current component may be removed from the DOM as a result. Any code after the call can still run, but you should not rely on the component staying alive.
Common errors
- Forgetting the mixin. Calling
this[NavigationMixin.Navigate]on a class that extends plainLightningElementfails because the method does not exist. Wrap the base class:NavigationMixin(LightningElement). - Missing or undefined
recordId. If@api recordIdis not populated, for example when the component sits on a page that is not a record page, navigation fails or lands on an error page. Check the value before you callNavigate. - Wrong
objectApiName. Using the label (Invoice) instead of the API name (Invoice__c) sends the user to a not-found page. - Using
listwithoutfilterName. The list view cannot be resolved. Always provide afilterNameinstate. - Unprefixed custom state keys. Custom keys in
stateneed a namespace prefix such asc__. Unprefixed keys are dropped or cause an error. - Expecting a return value.
Navigateis not a Promise-based call. Do your save work first, then navigate.
Navigating after an operation
The most common real use is navigating after a record is created or deleted. After a delete, the record page no longer exists, so send the user to the object's home page. After a create, send them to the new record.
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { createRecord } from 'lightning/uiRecordApi';
export default class CreateAndOpen extends NavigationMixin(LightningElement) {
async handleCreate() {
const fields = { Name: 'Acme Corp' };
const account = await createRecord({ apiName: 'Account', fields });
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: account.id,
objectApiName: 'Account',
actionName: 'view'
}
});
}
}The createRecord call resolves with an object whose id property is the new record's ID. Navigation happens only after that Promise resolves, so the ID is always available.
Exercise
A component lets a user delete the Account it is shown on. After the delete succeeds, it calls Navigate with a standard__recordPage reference using the same recordId and actionName 'view'. What goes wrong, and what page reference should it use instead?
Show answer
The record no longer exists, so the record page cannot load and the user sees an error or not-found page. Navigate to a page that still exists, such as the object home: { type: 'standard__objectPage', attributes: { objectApiName: 'Account', actionName: 'home' } }.
Exercise
You want a button that opens the New Opportunity form with the Account already filled in from the current record. Which page type, action, and state property do you use?
Show answer
Use type 'standard__objectPage' with attributes objectApiName 'Opportunity' and actionName 'new'. Put the default in state.defaultFieldValues, built with encodeDefaultFieldValues({ AccountId: this.recordId }) from lightning/pageReferenceUtils.