[SalesForce] How to populate lightning:select with picklist field values

Everything I have read online so far suggests that one must manually declare picklist (option) values when creating a lightning:select component in an Lightning application. For us, this is less than ideal.

Before I throw in the towel on this idea, I thought I'd ask Stack Exchange. Is it possible yet to have a lightning:select component automatically populate with options declared in the bound field's picklist values?

Best Answer

The basic method today is to get the values from the server, then render the values with $A.createComponents.

doInit: function(component, event, helper) {
  var action = component.get("c.init");
  action.setParams({ ... });
  action.setCallback(this, function(result) {
    var options = [];
    result.getReturnValue().values.forEach(
      function(value) {
        options.push(['option', { label: value, value: value }]);
      });
    $A.createComponents(
      options,
      function(components) {
        component.find("mypicklist").set("v.options", components);
      });
    });
  $A.enqueueAction(action);
}

If that's too complicated, then you might consider using lightning:inputField, which is designed to work with lightning:recordEditForm.

There's no way to get lightning:select automatically populate with the options when bound to a field.

Related Topic