0

I'm trying to fetch picklist options from a custom picklist field on a custom object.

When I wrote up my code like below, it worked.

picklistPractice.html

<template>
    <div class="slds-box slds-theme_default">
        <lightning-combobox label="My Picklist" options={picklistValues}>

        </lightning-combobox>
    </div>
</template>
picklistPractice.js

import { LightningElement, wire } from 'lwc';
import { getPicklistValues, getObjectInfo } from 'lightning/uiObjectInfoApi';
import PROJECT_OBJECT from '@salesforce/schema/Project__c';
import DINNER_MENU_FIELD from '@salesforce/schema/Project__c.Dinner_Menu__c';

export default class PicklistPractice extends LightningElement {
    picklistValues = [];

    @wire(getObjectInfo, { objectApiName: PROJECT_OBJECT})
    projectObjectInfo;

    @wire(getPicklistValues, { 
        recordTypeId: '$projectObjectInfo.data.defaultRecordTypeId',
        fieldApiName: DINNER_MENU_FIELD
    })
    getDinnerMenuOptions({data, error}) {
        if (error) {
            // TODO: Error handling
            console.error(error)
        } else if (data) {
            this.picklistValues = [...data.values]
        }
    }
}

Result:
enter image description here

However, when I used a property instead of a function for the second @wire, it didn't work. Below is the js code that didn't work. html file is the same as above.

import { LightningElement, wire } from 'lwc';
import { getPicklistValues, getObjectInfo } from 'lightning/uiObjectInfoApi';
import PROJECT_OBJECT from '@salesforce/schema/Project__c';
import DINNER_MENU_FIELD from '@salesforce/schema/Project__c.Dinner_Menu__c';

export default class PicklistPractice extends LightningElement {

    @wire(getObjectInfo, { objectApiName: PROJECT_OBJECT})
    projectObjectInfo;

    @wire(getPicklistValues, { 
        recordTypeId: '$projectObjectInfo.data.defaultRecordTypeId',
        fieldApiName: DINNER_MENU_FIELD
    })
    picklistValues;
}

Can anyone let me know why the second way is not working? I've seen a few articles like Get picklist values after dynamically fetching the record type id in lwc but I wasn't able to figure out why mine didn't work.



Thanks for your answer! Actually I tried to use picklistValues.data.values before posting this question ;).. See below... It threw an error.

enter image description here

<template>
    <div class="slds-box slds-theme_default">
        <lightning-combobox label="My Picklist" options={picklistValues.data.values}>

        </lightning-combobox>
    </div>
</template>
import { LightningElement, wire } from 'lwc';
import { getPicklistValues, getObjectInfo } from 'lightning/uiObjectInfoApi';
import PROJECT_OBJECT from '@salesforce/schema/Project__c';
import DINNER_MENU_FIELD from '@salesforce/schema/Project__c.Dinner_Menu__c';

export default class PicklistPractice extends LightningElement {

    @wire(getObjectInfo, { objectApiName: PROJECT_OBJECT})
    projectObjectInfo;

    @wire(getPicklistValues, { 
        recordTypeId: '$projectObjectInfo.data.defaultRecordTypeId',
        fieldApiName: DINNER_MENU_FIELD
    })
    picklistValues;
}

This isn't a huge problem for me right now because I can use a function instead of the property, but I'm wondering WHY NOT as an LWC newbie :).

4
  • 1
    Try updating the options porperty on your combobox component to something like: options={picklistValues.data.values}
    – McCubo
    Oct 17, 2023 at 7:55
  • @McCubo - Thanks for your answer! I actually used that before posting this question.. but it didn't work as well. It threw an error.. I appended some additional contents in my question body.
    – sfdcNewbie
    Oct 18, 2023 at 0:16
  • Try wrapping up your combobox component in a if template like this one: <template lwc:if={picklistValues.data}>
    – McCubo
    Oct 18, 2023 at 6:30
  • @McCubo Thanks!! That worked perfectly. I might have to look for how LWC manages a component's lifecycle. Thanks again for your answer!
    – sfdcNewbie
    Oct 19, 2023 at 0:03

1 Answer 1

0

See how the wired picklistValues property is used in this LWC recipe:

 <template for:each={picklistValues.data.values} for:item="item">

Personally, I always use a function so that the property is directly set to the value/values I want. That also allows any errors to be surfaced.

2
  • Thanks Keith! I've already seen that recipe but it didn't work for me as well. Updated my question with the error screenshot when I used picklistValues.data.values. Though this is not a huge problem for me right now because I can use a function, I simply wanted to know why it didn't work.. :)
    – sfdcNewbie
    Oct 18, 2023 at 0:22
  • FYI @MinsungKang, I was surprised to see data in that. Would have expected picklistValues.values.
    – Keith C
    Oct 18, 2023 at 8:56

You must log in to answer this question.

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