[SalesForce] Lightning Web Component: How to Get Picklist Values when recordTypeId is null

The lighting web component documentation shows how to get picklist values with the getPicklistValues wire adapter. Following this and other examples, I have come up with the below code.

The getPicklistValues adapter requires a recordTypeId value. Documenation says:

recordTypeId—(Required) The ID of the record type. Use the Object Info defaultRecordTypeId property, which is returned from getObjectInfo or getRecordUi

There are situations where defaultRecordTypeId returns null when using the getObjectInfo adapter. For example, in my case when trying to get the values for a picklist on the Opportunity Line Item Object. When the defaultRecordTypeId is null, I am finding that it crashes the component when loading.

I am wondering if this is a limitation of the getPicklistValues adapter or if I am missing some workaround. Thanks for your input!

/*eslint-disable no-console*/
import { LightningElement, wire, track } from 'lwc';
import { getPicklistValues, getObjectInfo } from 'lightning/uiObjectInfoApi'; 
import SuppliesLeftUOM_Field from '@salesforce/schema/OpportunityLineItem.SuppliesLeftUOM__c'; 
import OpptyLiObject from '@salesforce/schema/OpportunityLineItem'; 

export default class aTemp extends LightningElement {
    @track picklistValues;

    // GET OBJECT INFO
    @wire (getObjectInfo, {objectApiName: OpptyLiObject})
    objectInfo({
        error,data
    })
    {if(data){
        console.log(data); 
    } else if(error){
        console.log(error);
    }}  

    // GET PICKLIST VALUES 
    @wire (getPicklistValues, {recordTypeId: '$objectInfo.data.defaultRecordTypeId', fieldApiName: SuppliesLeftUOM_Field})
    picklistValues({
        error,data
    })
    {if(data){
        this.picklistValues = data;
    } else if(error){
        console.log(error);
    }}  
}

Best Answer

To use getPicklistValues on OpportunityLineItem (and possibly other objects without record types) you can hard-code the master record type Id - 012000000000000AAA - like so:

@wire(
    getPicklistValues, 
    { recordTypeId: '012000000000000AAA', fieldApiName: PICKLIST_FIELD }
)
picklistValues;

This works for me in API 46.0. Hope this helps!

Related Topic