3


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

3
  • It doesn't seem that other than the defined attributes, anything else will work in this case.
    – Jayant Das
    Mar 19, 2019 at 18:43
  • Thanks, Jayant. I was afraid of that... I will need to figure out a work-around.
    – Jeff Swatt
    Mar 19, 2019 at 19:02
  • I have provided some more details as an answer and also posted an idea around this.
    – Jayant Das
    Mar 19, 2019 at 19:21

5 Answers 5

1

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.

1
  • Thanks for the advice. I ended up doing the aura components with the createRecord event, passing in the correct record type Id. For now that works. Hopefully, this idea gets implemented soon.
    – Jeff Swatt
    Mar 19, 2019 at 19:43
9

I did find a workaround/hack your welcome to try.

I looked at the URL when I Went to the account page, and hit the new button and I found a url that looks like this:

'/lightning/o/Account/new?useRecordTypeCheck=1&count=1'

So I tried:

this[NavigationMixin.Navigate]({
      type: 'standard__objectPage',
      attributes: {
        objectApiName: 'Account',
        actionName: 'new'
      },
      state: {
        useRecordTypeCheck: 1
      }
    });

Now when they hit my "Create Account" button it shows this: selecting account type

This might fulfill your requirement, give it a shot.

4

Follow the below procedure, to have a different record type at the time of creation of new record.

I have used capital R for RecordTypeID, it didn't work. when changed to small 'r' like this recordTypeID, showed the expected result. As per the @track SelectedValue, it will show the particular page layout.

import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';

export default class RecordTypeSelection extends NavigationMixin(LightningElement) {
@track selectedValue;

 handleChange(event) {
        this.selectedValue = event.detail.value;
    }


 const defaultValues = encodeDefaultFieldValues({
            Name: this.AccountName,
            BillingStreet: this.Street,
            BillingCity: this.City,
            BillingStateCode: this.StateCode,
            BillingCountryCode: this.CountryCode,
            BillingPostalCode: this.PostalCode

        });

        
        // Navigate to the Account home page
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Account',
                actionName: 'new'
            },
            state: {
                nooverride: '1',
                defaultFieldValues: defaultValues,
                recordTypeId: this.recordTypeValue //This format works as expected, tested by @harikris    
            }
        });
}
2
  • What does 'nooverride' do? Jan 19, 2022 at 8:13
  • recordTypeId this worked for me
    – karan
    Dec 27, 2022 at 17:22
1

Just in case anyone stumbles on this one. This is possible by passing record type id to the NavigationMixin.Navigate.

Please check the below code:

this[NavigationMixin.Navigate]({
        type: 'standard__objectPage',
        attributes: {
            objectApiName: 'Account',
            actionName: 'new'            
        },
        state: {
            recordTypeId: 'xxxxxxxxxxxxxx' // Record Type Id
        }
    });
0

try setting the recordType value as default value :

// How do I use rtId below?

import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';

    this[NavigationMixin.Navigate]({

       const defaultValues = encodeDefaultFieldValues({
            RecordTypeId: event.target.id,
        });

        type: 'standard__objectPage',
        attributes: {
            objectApiName: 'Application__c',
            actionName: 'new',
            nooverride: '1',
        },
         state: {
                defaultFieldValues: defaultValues
            }
    });

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .