LWC: Picklist without knowing recordTypeId

In lwc-recipes, there is a component wireGetPicklistValues explaining how to fetch picklist values. This is the code below:

import { LightningElement, wire } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import TYPE_FIELD from '@salesforce/schema/Account.Type';

export default class WireGetPicklistValues extends LightningElement {
    @wire(getPicklistValues, {
        recordTypeId: '012000000000000AAA',
        fieldApiName: TYPE_FIELD
    })
    picklistValues;
}

In the code above, recordTypeId value is hardcoded. I checked the documentation for getPicklistValues wire adapter.
https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.reference_wire_adapters_picklist_values

It’s mentioned that to get recordTypeId, use the Object Info defaultRecordTypeId property, which is returned from getObjectInfo or getRecordUi.

So I modified my code as below, but it’s still giving me runtime exception. Can anyone point me out where am I wrong?

import { LightningElement, wire, track } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import TYPE_FIELD from '@salesforce/schema/Account.Type';

import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';

export default class WireGetPicklistValues extends LightningElement {
    @wire(getPicklistValues, {
        recordTypeId: this.objectInfo.data.defaultRecordTypeId,
        fieldApiName: TYPE_FIELD
    })
    picklistValues;

    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
    objectInfo;

}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Based on a quick test, this is what worked for me. Because the attribute in a wired service only supports reactive variable, thus changing the format here seemed to have worked.

@wire(getPicklistValues, {
    recordTypeId: '$objectInfo.data.defaultRecordTypeId',
    fieldApiName: TYPE_FIELD
})

Additionally, this approach works fine even if there is No Record Type available for an Object. In such cases, this pulls the default picklist values for the respective field on that Object.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x