Sub topic 5
Understanding PageReference objects
This chapter assumes you can build a basic Lightning Web Component (LWC), use @api and dispatchEvent, and have applied NavigationMixin to a component. You should also know what a Salesforce record ID is.
What a PageReference is
A PageReference is a plain JavaScript object that describes a destination. It does not contain a URL. Salesforce turns the object into the right URL for the current environment, such as Lightning Experience, a Experience Cloud site, or the Salesforce mobile app. Because of this, your code keeps working if URL formats change.
Every PageReference has up to three properties:
| Property | Required | Purpose |
|---|---|---|
| `type` | Yes | The kind of page, such as `standard__recordPage`. |
| `attributes` | Yes | Values that identify the specific page, such as a record ID. |
| `state` | No | Extra key-value pairs, usually used for parameters like filters. Keys need a namespace prefix, and values must be strings. |
Type decides the attributes
The type value sets which attributes are valid. A record page needs a record ID and an action. An object page needs an object API name and an action.
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'
}
});
}
}Here type is standard__recordPage. The attributes object holds recordId, objectApiName, and actionName. There is no state, because this page needs no extra parameters.
Common page types
The types you will meet most often are listed below. Later chapters cover them in detail.
| Type | Opens |
|---|---|
| `standard__recordPage` | A record page (view, edit, or clone). |
| `standard__objectPage` | An object page (home, list, or new record form). |
| `standard__namedPage` | A named standard page, such as Home. |
| `standard__webPage` | An external web address. |
How a PageReference flows
The same object works with both Navigate and GenerateUrl. Navigate moves the user at once. GenerateUrl gives you a URL string for a link.
Common errors
- Putting a URL in the object. Describe the destination with
typeandattributes. Usestandard__webPageonly for outside addresses. - Using a type with the wrong attributes. Each type has its own attribute names.
- Passing a non-string in
state. Values instatemust be strings. - Reusing one object and changing it. Build a new object for each call, so earlier calls are not affected.
Exercise
A teammate writes { attributes: { objectApiName: 'Contact', actionName: 'new' } } and passes it to Navigate. What is missing, and what should it be to open the New Contact form?
Show answer
The type property is missing. It should be standard__objectPage, so the full object is { type: 'standard__objectPage', attributes: { objectApiName: 'Contact', actionName: 'new' } }.