7

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;

}
11

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.

| 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.