6

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);
    }}  
}
  • 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 '19 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 '19 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 '19 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 '19 at 17:24
  • 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 '19 at 8:19
3

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);
        }
    }  
}
  • Kevin - I tried your recommended code, but I am still receiving the same behavior. Any other thoughts? Thanks! – jmjcobs Jun 5 '19 at 14:39
  • @jmjcobs I recommend sharing a minimal sfdx project that reproduces the issue – Kevin Venkiteswaran Jul 11 '19 at 2:50
  • naming @track picklistValues and the function @wire picklistValues causes a conflict rename one or the other... – jordan.baucke Dec 3 '19 at 0:14
  • Good catch, updated – Kevin Venkiteswaran Dec 4 '19 at 5:31
1

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!

  • Thanks. This solution works. – vinny Jan 10 at 14:50

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.