8

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);
    }}  
}
5
  • I don't think it is not working because of not having a record type. I can confirm that this works fine even if you don't have a record type defined for your object, this approach will give you the picklist values of the field in consideration. What errors do you see?
    – Jayant Das
    May 30, 2019 at 16:31
  • @JayantDas -- I am not getting an error back. When I place the component on a record page and comment out the getPicklistValues adapter, the component renders as expected. When the getPicklistValues adapter is present, the component and the page will not load. The only thing I can think of that might be wrong with the getPicklistValues adapter is the recordTypeId.
    – jmjcobs
    May 30, 2019 at 17:06
  • Can you put some console.log statements and see if there's an issue there? Or what does the console log in general say? Again, I can say (with a working example that I have), that even if there's no record type for an object, the adapter works fine.
    – Jayant Das
    May 30, 2019 at 17:08
  • @JayantDas -- I updated the original question with updated code. In the console, the data in the getObjectInfo adapter is logged to the console, but there is nothing logged to the console after that. It appears as though the script stops running.
    – jmjcobs
    May 30, 2019 at 17:24
  • 1
    I think this is currently a limitation with the system. I'm finding the same issue with picklists on the "User" object. I'm lucky in that the picklists live on a few different objects, so I can compare the behaviours. When using GetObjectInfo on the User object, the defaultRecordTypeId attribute comes back null, so it doesn't seem to be possible to use getPicklistValues.
    – Bigears
    Aug 25, 2019 at 8:19

4 Answers 4

5

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!

1
  • Thanks. This solution works.
    – vinny
    Jan 10, 2020 at 14:50
4

The issue is that $objectInfo.data.defaultRecordTypeId never becomes non-undefined because this.objectInfo is a function.

Try the code below instead.

Also remember that @wire provisions a stream of values. If you wire to a function you need to "reset" all state in the if/else.

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;

    // GET PICKLIST VALUES 
    @wire (getPicklistValues, {recordTypeId: '$objectInfo.data.defaultRecordTypeId', fieldApiName: SuppliesLeftUOM_Field})
    wiredPicklistValues({ error, data }) {
        // reset values to handle eg data provisioned then error provisioned
        this.picklistValues = undefined;
        if (data) {
            this.picklistValues = data;
        } else if (error) {
            console.log(error);
        }
    }  
}
4
  • Kevin - I tried your recommended code, but I am still receiving the same behavior. Any other thoughts? Thanks!
    – jmjcobs
    Jun 5, 2019 at 14:39
  • @jmjcobs I recommend sharing a minimal sfdx project that reproduces the issue Jul 11, 2019 at 2:50
  • 1
    naming @track picklistValues and the function @wire picklistValues causes a conflict rename one or the other... Dec 3, 2019 at 0:14
  • 1
    Good catch, updated Dec 4, 2019 at 5:31
1

For anyone that comes across this issue in the future:

The issue seems to be specific to the object OpportuinityLineItem.

In order to retrieve picklist values for this object you need to provide the recordTypeId of the opportunity itself.

import { getPicklistValues, getObjectInfo } from 'lightning/uiObjectInfoApi'; 
import YOUR_PICKLIST_FIELD from '@salesforce/schema/OpportunityLineItem.PicklistField__c'; 
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity';

export default class aTemp extends LightningElement {
    picklistValues;

    // GET OBJECT INFO
    @wire (getObjectInfo, {objectApiName: OPPORTUNITY_OBJECT})
    opportunityObjectInfo  

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

I was having the same issue just now and it appears that the data object contains an array of values where the options for the combobox are contained. In other words I had to assign them like so: this.options = data.values;

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;

    // GET PICKLIST VALUES 
    @wire (getPicklistValues, {recordTypeId: '$objectInfo.data.defaultRecordTypeId', fieldApiName: SuppliesLeftUOM_Field})
    wiredPicklistValues({ error, data }) {
        // reset values to handle eg data provisioned then error provisioned
        this.picklistValues = undefined;
        if (data) {
            this.picklistValues = data.values;
        } else if (error) {
            console.log(error);
        }
    }  
}

You must log in to answer this question.

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