0

I have lightning-combobox for which the picklist values are retrieved through getPicklistValues. How can I set default value of this field as the first picklist value?

HTML:

<lightning-combobox id="myField"
                            value={value}
                            options={myFieldValues}
                            onchange={doOperation}
                            style="width: 30em ;">
</lightning-combobox>

JS:

@track myFieldValues;
value;
    @wire(getPicklistValues, {recordTypeId: '$objectInfo.data.defaultRecordTypeId',fieldApiName: MY_CUSTOM_FIELD })
    setMyFieldValues({data, error}){
        if(data){
            console.log("picklist data:"+data.values);
            this.myFieldValues= data.values;
            this.value = data[0].values;
        } else if(error){
            console.log(error);
        }
    };

1 Answer 1

1

To set the default value of the lightning-combobox as the first picklist value, you need to modify your JS code slightly. You need to assign the value property of the combobox to the value property of the first element in the data.values array, not the data[0].values. For example, you can use this code:

@track myFieldValues;
value;

@wire(getPicklistValues, {recordTypeId: '$objectInfo.data.defaultRecordTypeId',fieldApiName: MY_CUSTOM_FIELD })
setMyFieldValues({data, error}){
        if(data){
                console.log("picklist data:"+data.values);
                this.myFieldValues= data.values;
                // Assign the value property to the first picklist value
                this.value = data.values[0].value;
        } else if(error){
                console.log(error);
        }
};

This will set the default value of the combobox to the first picklist value that is retrieved by the getPicklistValues method.

You must log in to answer this question.

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