[SalesForce] Lightning Web Components: navigate to the ‘new’ object page passing in specific record type Id

I am using LWCs to display lightning cards in a list. The list shows information about Programs. Each Program card has a button, and this button needs to take the user to the 'new object' screen of the Application object. Here is my code:

applyToProgram(event) {
    // The recordTypeId is coming from the .html
    // I have verified that the value is correct.
    var rtId = event.target.id;

    // How do I use rtId below?
    this[NavigationMixin.Navigate]({
        type: 'standard__objectPage',
        attributes: {
            objectApiName: 'Application__c',
            actionName: 'new',
            nooverride: '1',
            recordTypeId: rtId  // This does not work
        }
    });
}

This is not working. The user is presented with the 'new object' screen for the object's default record type and NOT the one specified in rtId. I tried putting 'nooverride' and 'recordTypeId' in the state:{} argument, and I also renamed 'recordTypeId' to just 'recordType'. None of these worked.

Can anyone help me with the correct syntax? Is this even possible in LWCs?

Thanks,
Jeff

Best Answer

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.

Related Topic