Lookup field from external database

Hello,
Is it possible to create a lookup field populated with data from an external database?

For example, I want to create an entity, with a customerId field. The list of customers is stored in a table of an external database, so in my entity edit page I would like to show a lookup field with the customers list.
I know that currently is not possible to define associations between entiies from different datastores. It would be enough for me if I can store the customer Id value in my entity, obtained from this lookup field, but I don’t know how I could do it…

Thank you!

Thank you!!

Hello, Miguel!

The list of LookupField options can be specified arbitrarily using the setOptionsList(), setOptionsMap() and setOptionsEnum(), setOptionsDatasource() methods. You can find out more information in the documentation or see this online sample.

Also, value change listener can be added to LookupField, so you can update your entity according to LookupField value changes.

Example with options datasource


lookupField.setOptionsDatasource(customersDs);
lookupField.addValueChangeListener(e -> {
   Customer customer = (Customer) e.getValue();
   getItem().setCustomerId(customer != null ? customer.getId() : null);
});

Example with options map


Map<String, UUID> optionsMap = new HashMap<>();
optionsMap.put("John Doe", UUID.fromString("3a329154-f56c-693c-2d23-9befaac452e1"));
optionsMap.put("Stan Smith", UUID.fromString("4987896e-4226-fa8e-b688-86bcf1179f8b"));
optionsMap.put("Tomas Anderson", UUID.fromString("c6703fe8-55f7-7259-a195-fce008440400"));
lookupField.setOptionsMap(optionsMap);

lookupField.addValueChangeListener(e -> {
   getItem().setCustomerId(e.getValue() != null ? (UUID) e.getValue() : null);
});

Regards,

Gleb