Create New Related Record in LWC – Lightning Web Components

I have a custom object AP_Customer_Address__c which has a lookup relationship to Contact. I have an LWC on the contact record page which allows the creation of a new address for that contact. I use NavigationMixin.Navigate to create new address records. When I try to create an address, it asks me to select a contact. I want the contact to be auto-filled and read-only. I tried adding the contact's record Id in state and attributes, but it's not working.

Please guide. Thanks!

LWC Screenshot
LWC Screenshot

Auto Fill contact name using Contact ID
Auto Fill contact name

Code

this[NavigationMixin.Navigate]({
                type: 'standard__objectPage',
                attributes: {
                    recordId: this.recordId, //added record Id here
                    objectApiName: 'AP_Customer_Address__c',
                    actionName: 'new'
                },
                state: {
                    recordId: this.recordId //added record Id here
                }
            });

Best Answer

Pre-population is yet not supported in Lightning Web component yet for NavigationMixin functionality. I have raised an issue and an idea a long back. Yet it has not been delivered.

  1. set default value while navigate to new record
  2. Allow setting pre-default values in record form by navigation service in LWC

You need to look for other workarounds like using Aura component or Lightning Data Service to open a record form with pre-populated values.

Currently, LWC allows you to just set the recordtype value by default as of now.

showNewOpportunityForm(recordTypeId) {
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Opportunity',
                actionName: 'new',

            },
            state: {
                recordTypeId: recordTypeId,
            }
        });
    }

Update

In spring 20, this feature has been delivered by Salesforce.

See this link. According to it, We can do this as follow:-

this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Account',
                actionName: 'new'                
            },
            state : {
                nooverride: '1',
                defaultFieldValues:"Name=Salesforce,AccountNumber=A1,AnnualRevenue=37000,Phone=7055617159"
            }
});