6

how to navigate to new record create a page with some default values as we do in a lightning $A.get("e.force:createRecord") in LWC

I have tried the below code, but not working:

           this[NavigationMixin.Navigate]({
                type: 'standard__objectPage',
                attributes: {
                    objectApiName: 'Corrective_Action__c',
                    defaultFieldValues: {
                        "Key_Action__c" : this.principlerecid
                    },
                    recordTypeId : this.correctiveActionId,
                    actionName: 'new'
                }
            },true);
7

This is a kind of limitation in Lightning web Component. Setting default values isn't yet supported, and planned for an upcoming release.

I have created one idea on this:- Allow setting pre-default values in record form by navigation service in LWC

You can upvote this. For time being continue using force:createrecord in your aura Component.

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"
            }
});
| improve this answer | |
  • But how to create a record that does not use hard coded values, but fields from the currently displayed object? I guess we need more JS code for that? – Sander de Jong Mar 25 at 15:57
2

The ability to navigate to a new record with default values has been added to LEX, and ready for use in both Aura and LWC components as of Spring 20, as specified in the release notes.

There is a good example that can be found in Aura documentation right now, but the LWC documentation is still missing at this point, because although referenced in the docs, the link (https://developer.salesforce.com/docs/component-library/bundle/lightning-pageReferenceUtils/documentation) is currently broken. Hopefully this will be addressed soon.

For now, there is a recipe in the lwc-recipes sample app. Here is the JS code. Note use of lightning/pageReferenceUtils to encode the default values:

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

export default class NavToNewRecord extends NavigationMixin(LightningElement) {
    // below, we take an object and encode it to work with navigation
    navigateToNewContactWithDefaults() {
        const defaultValues = encodeDefaultFieldValues({
            FirstName: 'Morag',
            LastName: 'de Fault',
            LeadSource: 'Other'
        });

        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Contact',
                actionName: 'new'
            },
            state: {
                defaultFieldValues: defaultValues // and here we set defaults as a nav parameter
            }
        });
    }
}

One note, sfdx-lwc-jest currently has not implemented the required package lightning/pageReferenceUtils, so you will not be able to test without mocking this package. An issue has been raised in that repo. For now, the recipes app has left a commented-out-but-most-likely-working test in place.

| improve this answer | |
1

To construct a custom button or link that launches a new record with prepopulated field values, use this sample formula:

/lightning/o/Account/new?defaultFieldValues=
    Name={!URLENCODE(Account.Name)},
    OwnerId={!Account.OwnerId},
    AccountNumber={!Account.AccountNumber},
    NumberOfEmployees=35000,
    CustomCheckbox__c={!IF(Account.SomeCheckbox__c, true, false)}
| improve this answer | |
1

This is now supported in spring 20. Here is a code sample for you.

this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Account',
                actionName: 'new'                
            },
            state : {
                nooverride: '1',
                defaultFieldValues:"Name=Tushar Sharma,AccountNumber=55555,NumberOfEmployees=35000,Phone=9988776655,Site=https://newstechnologystuff.com/"
            }
        });

We can use state parameter with defaultFieldValues to provide default values.

You can find complete code with sample component here: Default Field Values using Lightning Web Components

| improve this answer | |
0

Salesforce adding support for it in Spring'20

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.