# How can developers navigate to object home views and record creation pages using NavigationMixin?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Navigation, NavigationMixin

Read it on the site: https://www.forcechallenges.com/interview-questions/how-can-developers-navigate-to-object-home-views-and-record-creation-pages-using-navigationmixin

## Answer

**Basics:** Hard-coded URLs break across Lightning Experience, Experience Cloud and mobile. A PageReference is a declarative JSON description of a destination; the framework turns it into the right URL for the current container. Import `NavigationMixin` from `lightning/navigation` and wrap the base class: `class X extends NavigationMixin(LightningElement)`.

**How it works:** `this[NavigationMixin.Navigate](ref)` navigates; `this[NavigationMixin.GenerateUrl](ref)` returns a Promise of a URL (for use in `href`). Common references: object home or list `{ type: 'standard__objectPage', attributes: { objectApiName: 'Account', actionName: 'home' | 'list' }, state: { filterName: 'Recent' } }`; new record `actionName: 'new'`; record `{ type: 'standard__recordPage', attributes: { recordId, objectApiName, actionName: 'view' | 'edit' | 'clone' } }`; related list `standard__recordRelationshipPage` with `relationshipApiName`; plus `standard__navItemPage` (custom tab), `standard__namedPage`, `standard__webPage` and `comm__namedPage` for sites.

**Advanced / deep dive:** Pre-populate a new record using `encodeDefaultFieldValues` from `lightning/pageReferenceUtils` and passing `state: { defaultFieldValues: ... }`. The optional second argument `true` replaces the current history entry instead of pushing a new one. `CurrentPageReference` wire (also in `lightning/navigation`) reads state and parameters of the current page. Navigation inside Flow screens must use the flow navigation events rather than NavigationMixin. In Jest, the mixin is stubbed; verify behavior by mocking `Navigate`. Page reference types and `state` properties differ between Lightning Experience, mobile and Experience Cloud (LWR vs Aura sites), so test in each. Keep `state` keys prefixed with a namespace (e.g., `c__`) for custom parameters, since non-prefixed keys may be ignored or reserved.

**Code / config example:**
```javascript
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';

export default class NavDemo extends NavigationMixin(LightningElement) {
    goToAccountList() {
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: { objectApiName: 'Account', actionName: 'list' },
            state: { filterName: 'Recent' }
        });
    }
    newContact() {
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: { objectApiName: 'Contact', actionName: 'new' },
            state: { defaultFieldValues: encodeDefaultFieldValues({ LastName: 'Smith' }) }
        });
    }
}
```

**Hidden gotchas & must-remember points:**
- You must extend `NavigationMixin(LightningElement)`; otherwise `Navigate` is undefined.
- `GenerateUrl` is asynchronous.
- Use `filterName` with a list view API name or standard filter such as `Recent`.
- Custom state parameters need the `c__` prefix.
- Page reference support differs by container (LEX, mobile, Experience Cloud).
- Do not build URLs by string concatenation.

**Best practice:** Always navigate through PageReferences, and create URLs with `GenerateUrl` when you need links.

---
Source: ForceChallenges · last updated 2026-11-02
