Commit multiple instances of entity from programmatically created input fields

I am attempting to create a form on a screen that commits a batch of entities.
The entity is FirebasePendingInvite which is table that stores an email address, a group and a role.

I would like for a user to enter multiple emails with the corresponding group and role and be able to add as many sets of the these three fields as they please. Below is how I create the fields and add them to the form.
private void addFields() {

    TextField<String> email = uiComponents.create(TextField.TYPE_STRING);
    email.setId("emailField");
    email.setCaption("Email");
    email.setValueSource(new ContainerValueSource<>(firebasePendingInvitesDc, "email"));

    LookupField<Group> groupField = uiComponents.create(LookupField.of(Group.class));
    groupField.setId("groupField");
    groupField.setCaption("Group");
    groupField.setNullOptionVisible(false);
    groupField.setValueSource(new ContainerValueSource<>(firebasePendingInvitesDc, "group"));
    groupField.setOptions(new ContainerOptions<>(groupsDc));
    groupField.setValue(groupsDc.getItems().get(0));

    LookupField<Role> roleField = uiComponents.create(LookupField.of(Role.class));
    roleField.setId("roleField");
    roleField.setCaption("Role");
    roleField.setNullOptionVisible(false);
    roleField.setValueSource(new ContainerValueSource<>(firebasePendingInvitesDc, "role"));
    roleField.setOptions(new ContainerOptions<>(rolesDc));
    roleField.setValue(rolesDc.getItems().get(0));

    firebasePendingInvitesForm.add(email, 0);
    firebasePendingInvitesForm.add(groupField, 1);
    firebasePendingInvitesForm.add(roleField, 2);
}

I can successfully commit one entity but when I add multiple they all reflect the same values due to the ContainerValueSource<>

Is it possible to programmatically create multiple Instances/CollectionContainers for each set of corresponding fields(email, role, group)? Below is an example of the screen with multiple fields.

firebasependinginvites.zip (1.9 KB)

Hi.
As I understand you can use the standard BulkEditor component, which enables changing attribute values for several entity instances at once.

Note that BulkEditor works only in screens based on legacy API. Similar functionality for the current API is provided by the BulkEditAction.

Regards,
Natalia

1 Like

Natalia thank you for responding.

After looking through the BulkEditAction documentation I am under the impression it will only work for entity instances that have already been created in the DB and is being displayed in a table. I am having trouble identifying a method of using the BulkEditAction to create completely new entity instances.

What I would like to achieve is an ‘Edit screen’ that can create more than one entity instance at a time.

@firstova and others who come across this I believe I have solved my problem. When I create the three sets of fields and add them to the form, I create a new Instance Container, for the entity I wish to create, then bind each of the field’s Value Source to the newly created Instance Container. Below is the function I used to add the fields to the form and create the Instance Container.

private void addFields() {

    InstanceContainer<FirebasePendingInvites> firebasePendingInvitesDc;
    firebasePendingInvitesDc = dataComponents.createInstanceContainer(FirebasePendingInvites.class);
    FirebasePendingInvites firebasePendingInvites = getScreenData().getDataContext().create(FirebasePendingInvites.class);
    firebasePendingInvitesDc.setItem(firebasePendingInvites);

    TextField<String> email = uiComponents.create(TextField.TYPE_STRING);
    email.setId("emailField");
    email.setCaption("Email");
    email.setValueSource(new ContainerValueSource<>(firebasePendingInvitesDc, "email"));

    LookupField<Group> groupField = uiComponents.create(LookupField.of(Group.class));
    groupField.setId("groupField");
    groupField.setCaption("Group");
    groupField.setNullOptionVisible(false);
    groupField.setValueSource(new ContainerValueSource<>(firebasePendingInvitesDc, "group"));
    groupField.setOptions(new ContainerOptions<>(groupsDc));
    groupField.setValue(groupsDc.getItems().get(0));

    LookupField<Role> roleField = uiComponents.create(LookupField.of(Role.class));
    roleField.setId("roleField");
    roleField.setCaption("Role");
    roleField.setNullOptionVisible(false);
    roleField.setValueSource(new ContainerValueSource<>(firebasePendingInvitesDc, "role"));
    roleField.setOptions(new ContainerOptions<>(rolesDc));
    roleField.setValue(rolesDc.getItems().get(0));

    firebasePendingInvitesForm.add(email, 0);
    firebasePendingInvitesForm.add(groupField, 1);
    firebasePendingInvitesForm.add(roleField, 2);
}

Hope this helps somebody!