Validation script calculation remains a step behind
Hello. I'm working on a project that has rows of 3 text fields for which the values must be added and as a total for that row in a fourth field. This goes on for many lines. Something like this:
[total.0] = [A.0] + [B.0] + [C.0]
[total.1] = [A.1] + [B.1] + [C.1]
[total.2] = [A.2] + [B.2] + [C.2]
and so on.
I wanted a script that calculated only the appropriate totals when one of the corresponding values was changed, instead of recalculating everything all the time.
It was suggested to me that I should use a validation script, so that it would only run when a value was committed to that text field. I know very little of Javascript, but after reading around a bit, I had this set up as a document level script:
function CalcTotals()
{
//I believe this gives me the number post "." on the field name, which will correspond to each row that needs to be added up
var N= event.target.name.split(".").pop();
//The next 3 variables are supposed to give me the values of A, B and C corresponding to the appropriate row
var Va = +getField("A." + N).value;
var Vb = +getField("B." + N).value;
var Vc = +getField("C." + N).value;
//Then I'd add up that row
var T = Va + Vb + Vc;
//And assign it to the corresponding total field
getField("total." + N).value = T;
}
After this, I tested it out, running CalcTotals(); as a custom validation script in first A, B and C fields.
The issue I'm having is that, while total.o does become the sum of A.0, B.0 and C.0, it does so a step behind. like so:
starting with everything zero:
A.0 = 0
B.0 = 0
C.0 = 0
total.0 = 0
Change one value and the total remains zero
A.0 = 3
B.0 = 0
C.0 = 0
total.0 = 0
Change a second value, and the total becomes the previously correct sum:
A.0 = 3
B.0 = 0
C.0 = 2
total.0 = 3
A third change would give a total of the result after the second change:
A.0 = 3
B.0 = 7
C.0 = 2
total.0 = 5
And so on. The calculation is always one changed value behind. Any ideas on how to fix that, while keeping the calculation happening only for the appropriate roll when one the values is changed? Preferably with a document level script that I can just call as appropriate, instead of having to write individual scripts for each roll.
