1


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

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

| improve this answer | |
  • 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 '19 at 19:43
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
            }
    });
| 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.