Friday, February 7, 2020

Create record with default field values/pre-populate field values in Lightning Web Component(lwc)

Form Spring'20 onwards you can create the record with default field values.

This change doesn’t apply to Lightning Out, Lightning communities, or the Salesforce mobile app

Demo
HTML Code
<template>
    <lightning-card>
        <div class="slds-m-around_medium">
            <p>Create Record with pre-populate values in LWC <b></b></p>  
            <lightning-button label="Create Account" variant="brand" onclick={createRecordWithDefaultValues}></lightning-button>
        </div>
    </lightning-card>
</template>

Javascript Controller
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class CreateRecordInLWC extends NavigationMixin(LightningElement) {

    createRecordWithDefaultValues() {
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Account',
                actionName: 'new'                
            },
            state : {
                nooverride: '1',
                defaultFieldValues:"Name=Test LWC Acc,AccountNumber=123456,Rating=Hot,Phone=1234568975"
            }
        });
    }
}

Output

1 comment:

  1. it is possible anybody used this and hot success output

    ReplyDelete