20

According to the documentation here in order to retrieve a list of picklist values for an object we need to pass in a Record Type Id which can be retrieved using the getObjectInfo method.

The issue is that these are both wire functions and I cannot figure out how to chain these methods.

If I try code like the following:

getObjectInfo({ objectApiName: ACCOUNT_OBJECT })
            .then(result => {
                return getPicklistValues({recordTypeId: result.defaultRecordTypeId, fieldApiName: TYPE_FIELD});
            })
            .then(result => {
                this.picklistValues = result.data
            })
            .catch(error => {
                this.error = error;
            });

I get an error

render threw an error in 'c:getpicklistexample' [Imperative use is not supported. Use @wire(getObjectInfo).]

If I try something like

    @track rtId;

    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
    handleResult({error, data}) {
        if(data) {
            this.rtId = data.defaultRecordTypeId;
        }
    }

    @wire(getPicklistValues, {recordTypeId: this.rtId,fieldApiName: TYPE_FIELD})
    picklistValues;

Then it throws an internal server error. Anybody have an idea as to how I could retrieve the record type Id and then call to get the pick list values like the documentation suggests?

FYI my imports are:

import { LightningElement, wire, track } from 'lwc';
import { getPicklistValues, getObjectInfo } from 'lightning/uiObjectInfoApi';
import TYPE_FIELD from '@salesforce/schema/Account.Type';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
2
  • FYI, aware I could probably do this with apex but the docs suggest that I should be able to do this otherwise.
    – pbattisson
    Mar 30, 2019 at 16:45
  • Additional info - this was my test for a component using tasks instead of Accounts (I was trying to recreate using some of the sample code from the recipes). Worth noting that Task is not a supported object in the UI API which also was tripping me up.
    – pbattisson
    Mar 31, 2019 at 14:26

1 Answer 1

26

You have to use dynamic binding:

@wire(getPicklistValues, {recordTypeId: "$rtId", fieldApiName: TYPE_FIELD})
picklistValues;

This is mentioned in Use the Wire Service to Get Data.

I wrote a mockup that demonstrates this. You should be able to copy-paste this code directly.


<template>
    <template if:true={hasPicklistValues}>
        <select>
            <template for:each={picklistValues.data.values} for:item="entry">
                <option key={entry.value} value={entry.label}>{entry.label}</option>
            </template>
        </select>
    </template>
</template>

import { LightningElement, wire, track } from 'lwc';
import { getPicklistValues, getObjectInfo } from 'lightning/uiObjectInfoApi';
import TYPE_FIELD from '@salesforce/schema/Account.Type';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';


export default class Democomponent extends LightningElement {
    @track rtId;
    @track error;

    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
    handleResult({error, data}) {
        if(data) {
            this.rtId = data.defaultRecordTypeId;
        } else {
            this.error = error;
        }
    }

    @wire(getPicklistValues, {recordTypeId: "$rtId", fieldApiName: TYPE_FIELD})
    picklistValues;

    get hasPicklistValues() {
        return this.picklistValues && this.picklistValues.data && this.picklistValues.data.values;
    }
}
7
  • 2
    This is also how I would approach this (been writing LWC since it ga-ed). One trick for @wire is that it's reactive off value change and null to value is a perfectly valid reaction. My suspicion is that under the hood, it's a hash check on the property value.
    – tsalb
    Mar 30, 2019 at 17:19
  • @tsalb Thanks for confirming. I ended up writing up a mock for this anyways, it wasn't terribly bad.
    – sfdcfox
    Mar 30, 2019 at 18:06
  • Such is the speed of implementation when using ui*API and LWC ;)
    – tsalb
    Mar 30, 2019 at 18:25
  • 1
    And there is the thing I was missing. Thanks @sfdcfox for the answer.
    – pbattisson
    Mar 30, 2019 at 20:45
  • 2
    FYI @pbattisson @track is not necessary unless you want to bring it back to the template. You can actually assign it to a private _rtId and use $_rtId in the wire.
    – tsalb
    Mar 31, 2019 at 20:08

You must log in to answer this question.

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