How to calculate total amount

Hi all,
I need to calculate a total value for a master entity that has four slave entity.
It’s like an sales order with more rows.
I created a field in the master edit form with where I need to write the sum of all amount exiting in all four slave entity linked to the first.
But I don’t have any idea the way to solve this problem.
Do you have an example to understand a way to solve my question?
thank you

1 Like

Hi Corrado,

As I understand you have two separate screens Browser and Editor and you would like to calculate some aggregated value for the Entity in its editor. So, you can override the ‘’‘preCommit’’’ method in your editor and set the aggregated value to the corresponding field. See an example below:

public class OrderItemEdit extends AbstractEditor<OrderItem> {

    protected boolean preCommit() {
        BigDecimal subTotal = getItem()
        return super.preCommit();

If I got you wrong, could you please elaborate the problem? A sample project, illustrating your question would be very helpful.



1 Like

Hi Aleksey,
thank you so much for your answer.
but I’m not sure to understand your suggestion.
Please read my document attached.
thank you

Edit form.docx (790.8K)


Finally, got your question. I created a sample application and attached to this post. Open it in CUBA Studio and have a look at the


controller of the


screen. It sums the


fields of each entity instance in two compositions.


Aleksey (42.2K)

tomorrow morning I’ll try immediatly.
thank you again.

Welcome! The latest version of the sample is available here.

HI Aleksey,
I’m tring to add your code in my project.
But I have an issue when I try to enable the following string
payrolldetailsnew1Ds.addCollectionChangeListener(e -> getItem().setTotalAmount(calculateTotalAmount()));

this is the message when I try to execute the project.

:app-web:compileJavaC:\Users\schiavoc\studio-projects\pafootball\modules\web\src\com\palermocalcio\pafootball\web\payrollmaster_new\ error: ‘void’ type not allowed here
payrolldetailsnew1Ds.addCollectionChangeListener(e -> getItem().setTotalAmount(calculateTotalAmount()));
Note: C:\Users\schiavoc\studio-projects\pafootball\modules\web\src\com\palermocalcio\pafootball\web\utilities\ uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error


Hm… Seems


is declared as a


method. Make it to return the value, same type as the


field, it should solve the problem.


your solution is working.
thank you again.
can I make other question?

Sure, just start a separate topic.

This is my actual issue.
in the totalamount field (that now is working) I need to calculate the sign.
For each of forth slave entity I have the ‘amount’ field and the ‘sign’ field has two possible value ‘D’ for debit sign and ‘A’ for the credit sign.
So, if the sign is equal to ‘D’ the amount is positive. If the sign is equal to ‘A’ the amount is negative.

So, what’s the problem? Just change the logic in the


method. Modify lambdas to a simple


loop, check each item to have ‘D’ or ‘A’ type and do




, depending on the check result.

This is nothing to do with CUBA, just general java. We do provide project-related support in case if you need the help of our specialists. You can find more information about consultancy service here.


Hi Alekesy,
If I need to have a support to develop a single part of calculation like a sum or a duplicate copy or other in my application is there someone that help me to find a solution and solve my issue?
thank you.