Sub topic 9
State, list views, and default field values
This chapter assumes you can call this[NavigationMixin.Navigate] with a PageReference and know the standard__objectPage type from the previous chapters. Here you add the state property to a page reference. It changes how the target page opens.
What state is
A PageReference has a type, attributes, and an optional state. The attributes identify the page. The state adjusts it, for example by filtering a list or pre-filling a form. State is a flat object, and every value must be a string.
Opening a list view
To open an object's list view, use actionName: 'list' and set filterName in the state. The value is the list view's API name or ID. Recent is a standard list view that exists for most objects.
Pre-filling a new record form
To open the New form with some fields filled in, use actionName: 'new' and set defaultFieldValues in the state. The platform expects a single encoded string, not an object. Use encodeDefaultFieldValues from lightning/pageReferenceUtils to build it.
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
export default class AccountShortcuts extends NavigationMixin(LightningElement) {
@api recordId;
handleOpenList() {
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Contact',
actionName: 'list'
},
state: {
filterName: 'Recent'
}
});
}
handleNewContact() {
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Contact',
actionName: 'new'
},
state: {
defaultFieldValues: encodeDefaultFieldValues({
AccountId: this.recordId,
Title: 'Buyer'
})
}
});
}
}The keys in defaultFieldValues are field API names. The user can still edit the pre-filled values before saving.
Common state properties
| Property | Used with | Effect |
|---|---|---|
| `filterName` | Object page, `list` | Selects the list view |
| `defaultFieldValues` | Object page, `new` | Pre-fills fields on the New form |
| `c__yourKey` | Pages that host your own components | Passes a custom value |
Reading state back
A component on the target page can read the state through the CurrentPageReference wire adapter, which the next chapters cover. Here is the shape you will see.
import { LightningElement, wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
export default class ReadState extends LightningElement {
source;
@wire(CurrentPageReference)
setPageRef(pageRef) {
this.source = pageRef?.state?.c__source;
}
}Common errors
- Passing an object to
defaultFieldValuesinstead of an encoded string. The form opens without the values. - Passing a number or boolean as a state value. Convert it to a string first.
- Using a misspelled or non-existent
filterName. The page falls back to a default list view or shows an error. - Using a custom state key without
c__. The value may not arrive.
Exercise
You want a button on an Account page that opens the New Opportunity form with the AccountId field already set to the current account. Which page reference type and action do you use, and how do you supply the value?
Show answer
Use type: 'standard__objectPage' with objectApiName: 'Opportunity' and actionName: 'new'. In state, set defaultFieldValues to encodeDefaultFieldValues({ AccountId: this.recordId }), imported from lightning/pageReferenceUtils.