Sub topic 6
Navigating to record and object pages
This chapter assumes you can build a basic LWC and that you know NavigationMixin and the idea of a PageReference from the previous chapters. It covers the two page types you will use most: record pages and object pages.
The two page types
A PageReference is a plain object with a type, an attributes object, and an optional state object. You pass it to this[NavigationMixin.Navigate]. Two types cover most navigation in a record-centered app:
standard__recordPageopens a single record.standard__objectPageopens something about an object as a whole, such as its home page, a list view, or the New form.
The actionName attribute chooses what the user sees:
| Type | actionName | Result |
|---|---|---|
| `standard__recordPage` | `view` | The record detail page |
| `standard__recordPage` | `edit` | The record edit form |
| `standard__recordPage` | `clone` | The clone form for the record |
| `standard__objectPage` | `home` | The object's home page |
| `standard__objectPage` | `list` | A list view of the object |
| `standard__objectPage` | `new` | The New record form |
Opening a record and creating a contact
The component below applies the mixin by wrapping LightningElement. Without this wrapper, NavigationMixin.Navigate has nothing to call.
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'
}
});
}
handleNewContact() {
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Contact',
actionName: 'new'
}
});
}
}handleView needs a recordId. Here it comes from @api recordId, which Lightning sets when the component sits on a record page. handleNewContact needs no ID because the page is about the object, not one record.
The list action also needs a list view, which you choose with a filterName value. The page state chapter covers that.
Common errors
this[NavigationMixin.Navigate]is not a function. The class extendsLightningElementdirectly. ExtendNavigationMixin(LightningElement)instead.- Nothing happens, or the page shows an error. Check the spelling of
type,actionName, andobjectApiName. A misspelled value is not caught when you write the code. It fails when the user clicks. recordIdisundefined.@api recordIdis only filled when the component is placed on a record page or opened from a record action. On an app page, you must pass the ID in yourself.- Using the wrong API name for custom objects.
objectApiNamemust be the full API name, for exampleInvoice__c, not the label. - Expecting a return value.
Navigatestarts the navigation and does not return a Promise you can wait on. Do work that must finish first, such as a save, before you call it.
Exercise
A component extends LightningElement and calls this[NavigationMixin.Navigate] with a standard__objectPage reference for Contact and actionName 'new'. It throws an error on click. What is the most likely cause, and what is the fix?
Show answer
The class never applied the mixin, so NavigationMixin.Navigate is not available on the instance. Import NavigationMixin from 'lightning/navigation' and declare the class as extends NavigationMixin(LightningElement).