Multiple lines form - How to validate the sum of a numeric {field}

Hello, I’m creating an accounting form, multiple rows and I would liek to validate the form based on the sum of a numeric field {dare} and not the single row. The event ONVALIDATE is appling the code for each row inserted in the form multiple rows.

I applied the following code to calculate the sum:

onScriptInit - I create a global variable

[somma] =0; (occur just once)

onValidate - I add to somma each value of the column {dare} and it’s working

[somma] = [somma] + {dare}; (occur onetime for each row)

Validation - I would like to validate like below code. I tried to insert on event ONAFTERINSSERTALL but it’s registering the form and appearing the message “somma incorretta”.

if([somma] != 0){
sc_error_message(“somma incorretta”);
}

I want that the form is giving error and not updating the form if the sum is not equal to zero.

Can you help me please?

Thank you so much.