My Aura component use the force:createRecord
event to display an prepopulated new record page. What is the counterpart in #LWC?
There is a createRecord thing but it does different things.
In the new salesforce release Spring'20, we have this privilege to set the predefined values. We can now make the field API names and their values as a string and pass it to pageReference.state.defaultFieldValues attribute on standard__objectPage page reference types.
You can have the look here - https://releasenotes.docs.salesforce.com/en-us/spring20/release-notes/rn_lc_navigate_to_record_dfv.htm
For instance -
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class Demo extends NavigationMixin(LightningElement) {
handleClick(){
let temp = {
type: 'standard__objectPage',
attributes: {
objectApiName: 'Account',
actionName: 'new'
},
state : {
nooverride: '1',
defaultFieldValues:"Name=Salesforce,AccountNumber=A1,AnnualRevenue=37000,Phone=7055617159"
}
};
this[NavigationMixin.Navigate](temp);
}
}
Note - Spring'20 is only available for sandboxes for now.
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)}
Release Notes: https://releasenotes.docs.salesforce.com/en-us/spring20/release-notes/rn_general_lex_navigate_to_record_dfv.htm
For LWC, Use the navigation service and define the page you’re navigating to using the PageReference object.
To navigate in Lightning Experience, Lightning Communities, and the Salesforce app, use the navigation service, lightning/navigation.
Instead of a URL, the navigation service uses a PageReference, which describes the page. Using a PageReference insulates your component from future changes to URL formats. It also allows your component to be used in multiple applications, each of which can use different URL formats.
A PageReference is a JavaScript object that describes the page type, its attributes, and the state of the page.
The navigation service supports different kinds of pages in Lightning. Each page reference type supports a different set of attributes and state properties. Both APIs support these page reference types.
This code shows examples of navigating to different types of pages in Lightning. These examples show how to create page reference objects of different types and navigate to those pages.
// navigationToPagesExample.js
import { LightningElement, wire } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class NavigationToPagesExample extends NavigationMixin(LightningElement) {
navigateToNewRecordPage() {
// Opens the new Account record modal
// to create an Account.
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Account',
actionName: 'new'
}
});
}
Reference:- Navigate to Pages, Records, and Lists and Migrate Events
update:- I have raised one issue regarding this and get to know that:-
Setting default values aren't supported yet and planned for an upcoming release.
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=Sanket,AccountNumber=125,AnnualRevenue=5000,Phone=7056565656"
}
});
force:createRecord
and its alternative in LWC. You may like to add how can you pre-populate fields using this approach, if possible.
Commented
Feb 20, 2019 at 16:01