Set default fields values on create record LWC Salesforce

by Rijwan Mohmmed
7 comments
set-default-fields-values-on-create-record-lwc-salesforce-techdicer

Hello friends, today I am going to discuss Set default fields values on create record LWC Salesforce. We can create records with default values while using standard record-form navigation.

Also, check this: Generate QR codes for Salesforce Object

set-default-fields-values-on-create-record-lwc-salesforce-output-techdicer
set-default-fields-values-on-create-record-lwc-salesforce-output-techdicer

Key Highlights :

  1. Setup default field values before opening the record form.

Code :

For the demo purpose, I have created a basic component that will populate the default values in the Contact record.

Lwcform.Html:

<template>
    <lightning-card title="Set default fields values on create record LWC Salesforce" icon-name="standard:contact">
        <div class="slds-m-around_medium">
            <lightning-button label="Create Contact" variant="brand" onclick={createRecordWithDefaultValues}></lightning-button>
        </div>
    </lightning-card>
</template>

Lwcform.JS:

import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';

export default class Lwcform extends NavigationMixin(LightningElement) {
    createRecordWithDefaultValues() {
       //Object will be having field names and values 
        const defaultValues = encodeDefaultFieldValues({
            FirstName: 'TechDicer',
            LastName: 'KeepLearning',
            LeadSource: 'Web',
            Email:'test@g.com',
            Title:'Techdicer'
        });

        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Contact',
                actionName: 'new'
            },
            state: {
                defaultFieldValues: defaultValues
            }
        });
    }
}

Output :

set-default-fields-values-on-create-record-lwc-salesforce-output-techdicer
set-default-fields-values-on-create-record-lwc-salesforce-output-techdicer

Reference :

  1. Navigate to a Record’s Create Page with Default Field Values
What’s your Reaction?
+1
1
+1
0
+1
1
+1
0
+1
0
+1
0

You may also like

7 comments

Mohammad Ahtesham June 5, 2022 - 8:28 am

Hi, this is Amazing Post

Reply
Rijwan Mohmmed June 6, 2022 - 12:10 pm

thank you @MOHAMMAD AHTESHAM

Reply
Mary August 11, 2022 - 11:23 am

Hi and thanks for your nice post. I wonder how to set a default value for a field with picklist data type based on a criteria? I mean conditional default value. For instance, we want that a picklist value default be XXX if field1 is YYY AND field2 is zzz.

Reply
Rijwan Mohmmed August 11, 2022 - 2:26 pm

Hi Mary, you can create a variable and put your conditions so you can get a variable value and then set it in encodeDefaultFieldValues({
in createRecordWithDefaultValues method
Like :
createRecordWithDefaultValues() {
let firstname = ”;
if(compName == ‘TechDicer’){
firstname = ‘TechDicer’;
}
//Object will be having field names and values
const defaultValues = encodeDefaultFieldValues({
FirstName: firstname,
LastName: ‘KeepLearning’,
LeadSource: ‘Web’,
Email:’test@g.com’,
Title:’Techdicer’
});

Reply
Mary August 12, 2022 - 11:38 am

Thanks Rijwan! I’ll try it.

Reply
Aashna September 19, 2023 - 11:33 am

Hi, Can we override, this record form’s Save button?

Reply
Rijwan Mohmmed September 19, 2023 - 4:38 pm

We can handle this save button click,
HTML

JS
onSubmitHandler(event) {
event.preventDefault();
// Get data from submitted form
const fields = event.detail.fields;
// Here you can execute any logic before submit
// and set or modify existing fields
fields.Name = fields.Name + fields.Phone
// You need to submit the form after modifications
this.template
.querySelector('lightning-record-edit-form').submit(fields);
}

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.