Navigate to the new record page for any object. Optionally pre-populate fields with default values using encodeDefaultFieldValues.
Source
force-app/main/default/lwc/navToNewRecord/navToNewRecord.js
force-app/main/default/lwc/navToNewRecordWithDefaults/navToNewRecordWithDefaults.js
Basic Usage
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class NavToNewRecord extends NavigationMixin(LightningElement) {
navigateToNewContact() {
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Contact',
actionName: 'new'
}
});
}
}
With Default Field Values
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
export default class NavToNewRecordWithDefaults extends NavigationMixin(
LightningElement
) {
navigateToNewContactWithDefaults() {
const defaultValues = encodeDefaultFieldValues({
FirstName: 'Morag',
LastName: 'de Fault',
LeadSource: 'Other'
});
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Contact',
actionName: 'new'
},
state: {
defaultFieldValues: defaultValues
}
});
}
}
NavigationMixin
Extend your component with NavigationMixin to access navigation capabilities:
export default class NavToNewRecord extends NavigationMixin(LightningElement) {
// component code
}
Set to standard__objectPage for object pages
The API name of the object (e.g., ‘Contact’, ‘Account’, ‘CustomObject__c’)
Set to new to display the new record creation page
Encoded default field values. Use encodeDefaultFieldValues() from lightning/pageReferenceUtils to encode an object of field name-value pairs
Default Field Values
Use encodeDefaultFieldValues to pre-populate form fields:
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
const defaultValues = encodeDefaultFieldValues({
FirstName: 'John',
LastName: 'Doe',
Email: '[email protected]',
LeadSource: 'Web'
});
Key Features
- Navigate to new record creation forms
- Pre-populate fields with default values
- Supports all standard and custom objects
- Type-safe navigation with PageReference