Navigate to a Record’s Create Page with Default Field Values
To launch a record’s create page with prepopulated field values, use lightning/pageReferenceUtils and lightning/navigation together.
The lightning/pageReferenceUtils module provides the encodeDefaultFieldValues() and decodeDefaultFieldValues() functions, which encode default field values into a string and decode them. Assign an encoded string to the pageReference.state.defaultFieldValues attribute in a standard__objectPage page reference.
With standard actions, the default field values pass through the URL to the object as a string, and the redirect and replace is handled for you. With override actions, you are responsible for decoding the string of default field values from the URL.
If you’re launching a flow from a Quick Action in a Salesforce Console app, and the flow contains a custom LWC that navigates to a record create page with default values, those default values are sometimes not saved.
Note
Launch a Contact Record with Default Field Values Using a Standard Action
This example launches a record’s create page with prepopulated default values.
This example HTML includes the link to create a contact.
1<!-- navToNewRecordWithDefaults.html -->2<template>3 <lightning-button4 name="new-with-defaults"5 label="Go to New Contact with Defaults"6 class="slds-m-around_medium"7 onclick={navigateToNewContactWithDefaults}>8 </lightning-button>9</template>
To encode the default field values into a string, pass them to encodeDefaultFieldValues(). Assign the encoded string to the state.defaultFieldValues property in the page reference.
To dynamically create or modify your default values before passing them to the Navigate function, specify the values in connectedCallback().
Here’s how you can update the previous example to send several dynamic fields and values.
1//navToNewRecordWithDynamicValues.js2import{LightningElement}from "lwc";3import{NavigationMixin}from "lightning/navigation";4import{encodeDefaultFieldValues}from "lightning/pageReferenceUtils";56export default class NavToNewRecordWithDynamicValues extends NavigationMixin(LightningElement){7 // An object that stores default values for the sObject8 obj = {9 FirstName: "Morag",10 LastName: "de Fault",11};1213 companyName = "Demo";14 newFieldName = "Company";1516 connectedCallback(){17 // Dynamically adding a static field name and value18 this.obj.LeadSource = "Other";19 this.obj.LastName = "Some other name";2021 // Dynamically adding a dynamic field name and value22 this.obj[this.newFieldName] = this.companyName;23}2425 navigateToNewContactWithDefaults(){26 const defaultValues = encodeDefaultFieldValues(this.obj);27}28}
Handle Default Field Values Using an Override Action
To override standard action behavior with a custom solution, wrap your module inside an Aura component.
With override actions, you are responsible for decoding the string of default field values from the URL.
This example uses CurrentPageReference to read the default field values from state and get the encoded string. It then passes the string to decodeDefaultFieldValues() to decode it and handle the account creation.
This example is similar to prepopulating field values using lightning-record-edit-form, except that here the defaultFieldValues are dynamically generated when navigating to the form.
To perform an override with currentPageReference.state.defaultFieldValues, encode the string with encodeDefaultFieldValues() in the same component, such as from connectedCallback(). See the previous sections for examples on encoding the string.
1// lwcNewAccountOverride.js2import{LightningElement, wire}from "lwc";3import{CurrentPageReference}from "lightning/navigation";4import{decodeDefaultFieldValues}from "lightning/pageReferenceUtils";56export default class LwcNewAccountOverride extends LightningElement{7 @wire(CurrentPageReference)8 setCurrentPageReference(currentPageReference){9 // defaultFieldValues must correspond to an encoded string10 if(currentPageReference.state.defaultFieldValues){11 const dfvObject = decodeDefaultFieldValues(currentPageReference.state.defaultFieldValues);12 this.dfv_AccountName = dfvObject.Name;13 this.dfv_NumberOfEmployees = dfvObject.NumberOfEmployees;14 // Handling required for boolean because we don't support boolean field types15 this.dfv_CustomCheckbox = dfvObject.CustomCheckbox__c === "true";16 this.dfv_OwnerId = dfvObject.OwnerId;17}18}1920 dfv_AccountName = "";21 dfv_NumberOfEmployees = "";22 dfv_OwnerId = "";23 dfv_CustomCheckbox = false;2425 // Run code when account is created26 handleAccountCreated(){}27}
All encoded default field values are passed to the record create page as strings. For example, 35000 is passed into the page as a string instead of a number, and the Boolean values true and false are passed as strings.
Important
To use this code, from Setup, enter Object Manager. On the Account object, create a checkbox field with the API name CustomCheckbox__c. Then select Buttons, Links, and Actions and edit the New action. For Lightning Experience Override, select Lightning component and select the c:auraOverrideWrapper component.