0

For a picklist field, it is possible within Object Manager, to select one picklist value as the default value, so that for new records this field is automatically selected in the dropdown.

I want to show a picklist field on a lwc using a lignting combobox. Is it possible using the lwc framework to assign the correct default picklist value?

I used:

import { getObjectInfo, getPicklistValues } from "lightning/uiObjectInfoApi";
picklistFieldValue;
...

@wire(getPicklistValues, {
        recordTypeId: "$someSObject.data.defaultRecordTypeId",
        fieldApiName: SOME_PICKLIST_FIELD,
    })
    setPicklistValues({ data, error }) {
        if (data) {
            //How to set the value to the default picklist value?
            ...
        }
    }
}

There however seems to be no method or field I can use that is provided. Any advice or help would be very much appreciated.

1 Answer 1

2

The default value, if any, will be in data.defaultValue. To avoid possible race conditions, store the value in a property:

if(data) {
  if(!this.comboboxValue) {
    this.comboboxValue = data.defaultValue.value;
  }
  ...
}

and bind to the value property:

<lightning-combobox options={options} value={comboboxValue} ...>
1
  • Thanks a lot @sfdcfox! This works indeed. Maybe good to refer to your code snap that you should assign data.defaultValue.value. Commented Feb 14 at 18:30

You must log in to answer this question.

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