Display custom text in lookupfield

Hi,
I have a lookupfield for selecting employee name. The Employee entity contains fields firstname, lastname, dept. I want to display employee name in the format in the lookupfield. How can i set the captionProperty to show this?

<collection id="empDc" class="com.test.Employee" view="_local">
            <loader id="empDl">
                <query><![CDATA[select e from test_Employee e]]></query>
            </loader>
        </collection>

 <instance id="empId" class="com.test.Employee"/>

<lookupField id="lookupFieldEmp" optionsContainer="empDc" caption="Select Employee"  dataContainer="empId" property="firstname"/>

If I populate the dropdown using lookupfield.setOptionsList(), i am not able to bind the selected employee name in the lookupfield to the Employee entity instance as the selected value will be a String.

Hi!

You can use setOptionCaptionProvider:

lookupField.setOptionCaptionProvider(employee -> {
    return employee.getName() + " " + employee.getLastName();
});

Or another way is to use setOptionsMap(Map<String, I>) where key is caption for value.