MultiTenant entity instance won't commit

Hello,

I’m working on a multi-tenant app that uses the multitenancy addon. All of my browser and editor screen work great together.

For each of my tenants, there will be an editor screen to provide company info. There will only be one company info record per tenant so my thought was to create a single editor screen for the company entity instead of going through a browser screen.

I added my editor screen to the web-menu.xml file to get it to show in the menu bar which works fine.

My company-edit screen has the following dataContainer and loader

<data>
        <instance id="companyDc"
                  class="com.bi.salessaas.entity.Company"
                  view="company-view-with-logo">
            <loader id="companyDl">
                <query><![CDATA[select e from salessaas_Company e
where e.tenantId = :tenantId]]></query>
            </loader>
        </instance>
    </data>

In the screen controller i removed the @LoadDataBeforeShow annotation and subscribed to the onBeforeShow event to load the data.

    @Subscribe
    private void onBeforeShow(BeforeShowEvent event) {
        String tenantId = multiTenancyTools.getCurrentUserTenantId();

            companyDl.setParameter("tenantId", tenantId);
            try {
                companyDl.load();

            }
            catch (EntityAccessException e){
                //entity is null, no company info record yet
                //defaults to creating a new instance so we can just continue
            }
    }

If there is no matching record returned, the editor screen creates a new entity instance to work with and i can commit that record with no issue.

My issue is, when there is an existing Company entity returned and its gets loaded, I get a null constraint error when i try to commit using the default OK button.

I dug into the dataContext object, and when it loads an existing entity, it keeps the new entity created on the screen load. Since it’s not replacing the new one with the existing one, when i try and commit it tries to save the new blank record and throws the null reference.

dataContext

I’m not sure how to get the dataLoader to replace any new instance with an existing one if its found.

Any help would be greatly appreciated!

Brian

Hi @Tech,

Could you please prepare and provide us a small project where this issue can be replicated?

Regards,
Evgeny

Hello Evengy,

Yes I can do that, whats the easiest way?

You can zip your project and attach to the message in this thread

Attached is the working directory minus the deploy folder since it’s too big
It’s also on
github GitHub - Bri2785/sales-saas

sales-saas.zip (1.5 MB)

Hi @Tech,

Please try to use following code to initialize tenant company

@Subscribe
    private void onBeforeShow(BeforeShowEvent event) {
        String tenantId = multiTenancyTools.getCurrentUserTenantId();

        Company newCompany = getEditedEntity();
        companyDl.setParameter("tenantId", tenantId);
        try {
            companyDl.load();
            getScreenData().getDataContext().evict(newCompany);
            setEntityToEdit(companyDc.getItem());
        } catch (EntityAccessException e) {
            //entity is null, no company info record yet
            //defaults to creating a new instance so we can just continue
        }
    }

Regards,
Evgeny

Hello Evgeny,

I’ll give it a shot, thanks for your help!

Hello Evgeny,

I upgraded to version 7.1.0 before testing this. For some reason, the direct link to the editor screen from the application menu is not firing any of the before show events on the screen controller any more. I have breakpoints set in the onBeforeShow event subscribed to the BeforeShowEvent, but they are never hit when using the menu link (<item screen="salessaas_Company.edit"/>).

If i create a browser screen for the Company Entity class and select and edit a company from the list, the events fire correctly. Any thoughts?

Hi, @Tech
Thank you for the sample project.
The problem is that(since platform 7.1) entityToEdit is not passed to the editor screen on its opening from the main menu.
We have created the issue: StandardEditor screen could not be opened from web menu · Issue #2449 · cuba-platform/cuba · GitHub
You can work around the problem as follows. Subscribe to the Init event and set the entityToEdit there.

@Subscribe
    private void onInit(InitEvent event) {
        String tenantId = multiTenancyTools.getCurrentUserTenantId();
        companyDl.setParameter("tenantId", tenantId);
        try {
            companyDl.load();
            Company curentItem = companyDc.getItem();

            setEntityToEdit(curentItem);

        } catch (EntityAccessException e) {

            String metaClassName = "salessaas_Company";
            setEntityToEdit((Company) metadata.create(metaClassName));

            //entity is null, no company info record yet
            //create a new instance
        }

    }

You can also create a legacy edit-screen for your Company entity. Legacy edit-screens should be opened OK from the main menu.
Regards.

This works great! Thanks for the update!