How to multiply twoo bigDecimal fields

I have a simple questions to which i can’t seem to find the answer.
I want to multiply two bigDecimal fields with each other and the result will be calculated in an other field.
I’ve been fighting with this for hours, but i haven’t found the solution.

Can someone please help?

Regards,

LS

An easy way to add calculated fields is to add them to the entity.

Let’s say you have a entity with two BigDecimal fields as follows:

@Column(name = "PRICE_PER_UNIT")
private BigDecimal pricePerUnit;

@Column(name = "AMOUNT")
private BigDecimal amount;

You can add a transient property by adding a getter and marking it as Transient and MetaProperty:

@Transient
@MetaProperty(related = {"pricePerUnit","amount"})
public BigDecimal getLineTotal() {
    return pricePerUnit == null || amount == null ? null : pricePerUnit.multiply(amount);
}

In the user interface you can now use property="lineTotal" to bind it to a component.

1 Like
@Inject
    private InstanceContainer<OrderLine> orderLineDc;

    @Subscribe(id = "orderLineDc", target = Target.DATA_CONTAINER)
    protected void onOrderLinesDcCollectionChange(CollectionContainer.CollectionChangeEvent<OrderLine> event) {
        if (event.getChangeType() != CollectionChangeType.REFRESH) {
            calculateSubAmount();
        }
    }

    protected void calculateSubAmount() {
        BigDecimal subAmount = BigDecimal.ZERO;
        for (OrderLine line : orderLineDc.getItem()) {
            subAmount = (line.getProduct().getPrice().multiply(line.getQuantity()));
        }
        getEditedEntity().setSubAmount(subAmount);
    }

}

This is my code, but it doesn’t work

If you want to react on attribute value changes, subscribe to the ItemPropertyChangeEvent.