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.

In the lwc-recipes sample repo, see the navToNewRecordWithDefaults component.

This example launches a record’s create page with prepopulated default values.

This example HTML includes the link to create a contact.

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.

To override standard action behavior with a custom solution, wrap your module inside an Aura component.

The lwcNewAccountOverride component includes a form for a new account record page that displays default field values.

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.

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.

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.

See Also