I have also posted this idea to allow for such a feature in LWC.
It seems as of today, you cannot specify a record type id in LWC while utilizing NavigationMixin.Navigate
to create a new record.
The attributes
mentioned for standard__objectPage
are only:
- actionName
- objectApiName
- filterName
There's no option available even on standard__recordPage
which you could have utilized here. With all my experiences, something not documented is less likely to work (even if it does, it's not officially supported and could break at any point).
Your only option here could be to utilize an aura component composition and be able to use force:createRecord
, as it does provide you a way to specify a recordTypeId
.
Added based on comments
The getter recordTypeId
will not be invoked unless you use it in HTML like {recordTypeId}
OR by invoking it in JS like this.recordTypeId()
.
$recordTypeId
is dynamic PROPERTY and not a method which will get value from method (getter).
You are not setting objectInfo
anywhere so recordTypeId()
will never return anything even if used in HTML.
Try below JS:
import { LightningElement, wire, api, track } from 'lwc';
import ORDER_OBJECT from '@salesforce/schema/Order';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import TYPE_FIELD from '@salesforce/schema/Order.Type';
export default class Poc extends LightningElement {
@wire(getObjectInfo, { objectApiName: ORDER_OBJECT })
getobjectInfo(result) {
if (result.data) {
const rtis = result.data.recordTypeInfos;
this.recordTypeId = Object.keys(rtis).find((rti) => rtis[rti].name === 'Asia');
}
}
@wire(getPicklistValues, { recordTypeId: '$recordTypeId', fieldApiName: TYPE_FIELD })
orderTypeOption;
}
previous answer
In your example I see that you are using objectInfo
but you are never fetching it.
Pls check below code sample:
import { LightningElement, wire, api } from 'lwc';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
export default class Poc extends LightningElement {
@wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
getobjectInfo(result) {
console.log('result => ', JSON.stringify(result.data.recordTypeInfos));
}
}
Here you will get the record types through result.data.recordTypeInfos
. You will see below data in recordTypeInfos
.
{
"012000000000000AAA": {
"available": true,
"defaultRecordTypeMapping": false,
"master": true,
"name": "Master",
"recordTypeId": "012000000000000AAA"
},
"0120K000000TO5FQAW": {
"available": true,
"defaultRecordTypeMapping": false,
"master": false,
"name": "P1r",
"recordTypeId": "0120K000000TO5FQAW"
},
"0120K000000TO5KQAW": {
"available": true,
"defaultRecordTypeMapping": true,
"master": false,
"name": "P2r",
"recordTypeId": "0120K000000TO5KQAW"
}
}
You can find more info here
Best Answer
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:-