0

I have been trying to get the picklist values dynamically in LWC without using the Apex Code but somehow I am unable to get them. Not sure what is the actual problem

import { getObjectInfo, getPicklistValues } from 'lightning/uiObjectInfoApi';
import USER_OBJECT from '@salesforce/schema/User';
import PROCESSING_USER_TEAM from '@salesforce/schema/User.Processing_User_Team__c';

export default class Wtclm_DocExecAllocation extends LightningElement {

    @track teams;
    selectedTeam = 'None';
    @api objectName = 'User';
    @api fieldName = 'Processing_User_Team__c';

    @wire(getObjectInfo, { objectApiName: USER_OBJECT })
    objectInfo;

    @wire(getPicklistValues, {
        recordTypeId: "$objectInfo.data.defaultRecordTypeId",
        fieldApiName: PROCESSING_USER_TEAM
    })
    pickValues({ error, data }) {
        if (data) {
            this.teams = data.values.map(plValue => {
                return {
                    label: plValue.label,
                    value: plValue.value
                };
            });

        } else if (error) {
            console.log(error);
        }
    }


<lightning-combobox
            name="teams"
            label="Teams"
            value={selectedTeam}
            placeholder="Select Team"
            options={teams}
            onchange={handleSelectedTeam} ></lightning-combobox>

1 Answer 1

1

The issue here is with dynamic and reactive object property $objectInfo.data.defaultRecordTypeId. From the Understand the Wire Service

In the wire adapter’s configuration object, prefix a value with $ to reference a property of the component instance. The $ prefix tells the wire service to treat it as a property of the class and evaluate it as this.propertyName

Reactive property allows LWC to handle changes done to that property and invoke wire function every time, this property is changed automatically. But this property should be a direct field of LWC class.

You need to store objectInfo.data.defaultRecordTypeId in a separate field.

import { getObjectInfo, getPicklistValues } from 'lightning/uiObjectInfoApi';
import USER_OBJECT from '@salesforce/schema/User';
import PROCESSING_USER_TEAM from '@salesforce/schema/User.Processing_User_Team__c';

export default class Wtclm_DocExecAllocation extends LightningElement {

    @track teams;
    selectedTeam = 'None';
    @api objectName = 'User';
    @api fieldName = 'Processing_User_Team__c';
    objectInfoData;
    defaultRecordTypeId;

    @wire(getObjectInfo, { objectApiName: USER_OBJECT })
    wireObjectInfo({ error, data }){
        if(data){
            this.objectInfoData = data; // if you still need it
            this.defaultRecordTypeId = data.defaultRecordTypeId;
        } else if (error) {
             //handle error
        }
    }

    @wire(getPicklistValues, {
        recordTypeId: '$defaultRecordTypeId',
        fieldApiName: PROCESSING_USER_TEAM
    })
    pickValues({ error, data }) {
        if (data) {
            this.teams = data.values.map(plValue => {
                return {
                    label: plValue.label,
                    value: plValue.value
                };
            });

        } else if (error) {
            console.log(error);
        }
    }
}
3
  • Thanks for your answer still it is not working I tried to see console of the return response of get object info and I am seeing that data.defaultRecordTypeId is coming as null for the user object. FYI there is no record type available for the User object except master. Commented Feb 1, 2023 at 9:34
  • @Sam-dev-sfdc makes sense, as we can't create recordtypes for User can you try to hardcode value 012000000000000AAA instead of $defaultRecordTypeId ? Commented Feb 1, 2023 at 12:44
  • I did try the same but it didn't work it was throwing an error Invalid record Type id. At last, I used the apex class to get the picklist value Commented Feb 3, 2023 at 10:05

You must log in to answer this question.

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