Auto-Fill Customer name to Project Editor when clicking create from Customer Editor.

I am looking for a solution to setting an initial value to a drop-down field on a project editor screen when clicking create from a previously selected related Customer editor screen (See attached picture). It should automatically populate with the customers name for the project editor when clicking create from the previous screen. Furthermore, when creating a project directly from a project browser screen, I would still like to be able to choose a customer for this drop down field, which I am able to do now. I hope this makes sense. Thanks for the help.

example

1 Like

Nevermind,
Solved it with the following code placed in my customer-edit controller:


package com.company.intranet.web.customer;

import com.haulmont.bali.util.ParamsMap;
import com.haulmont.cuba.core.global.PersistenceHelper;
import com.haulmont.cuba.gui.components.AbstractEditor;
import com.haulmont.cuba.gui.components.actions.CreateAction;
import com.haulmont.cuba.gui.components.actions.EditAction;
import com.company.intranet.entity.Customer;

import javax.inject.Named;
import java.util.Map;

public class CustomerEdit extends AbstractEditor<Customer> {

    @Named("projectsTable.create")
    private CreateAction projectsTableCreate;
    
    @Override
    public void init(Map<String, Object> params) {
        projectsTableCreate.setWindowId("intranet$Project.edit");
        // Disable notification on saving entity without closing the screen
        setShowSaveNotification(false);
    }    
    
    @Override
    protected void postInit() {
        // Get currently edited customer
        Customer customer = getItem();
        // Enable "create" action only if the edited customer is saved to the database
        projectsTableCreate.setEnabled(!PersistenceHelper.isNew(customer));
        // Pass the currently edited customer to be set to the "customer" attribute of a newly created project
        projectsTableCreate.setInitialValues(ParamsMap.of("customer", customer));
    }    
}

Thank you for posting the solution.