Level 1

0% to

Level 2

Tip /

to gain points, level up, and earn exciting badges like the new
Mission!

View all

## For loop not working - calculation works / count of rows works

markush32574551
Level 2

Likes

2 likes

Total Posts

21 posts

2 solutions
View profile

markush32574551
Level 2

Likes

2 likes

Total Posts

21 posts

2 solutions
View profile
markush32574551
Level 2

24-04-2018

Hey,

I am working on a form and I can't loop through a dynamic table.

I want to calculate following in a invisible row. The calculation is based on a dynamic row above.

See the hierarchy below:

my Script:

var All = 0;

// I also tried following:

// or (....; i <= parent.parent.resolveNodes("Kalk[*]") ...

//for (....; i <= parent.parent._Kalk.count; ...

for (i = 0; i <= parent.parent._Kalk.length ; i++) {

// The value of the total_einf cell consists of the working time (decimal; from - to - break) multiplied with the value of cell Quty_einf (=number of technicians)

// I need the overtime. Overtime starts at 10 working hours so I subtract 10 hours multiplied with the Quty_einf (number of techn.) from the total sum

//Then I get the total overtime

var Rowval = xfa.resolveNode("Kalk[" + i + "].Total_einf").rawValue - (10 * xfa.resolveNode("Kalk[" + i + "].Quty_einf").rawValue);

//I check if overtime was made

if (Rowval > 0) {

//Now I want to add the value

var All =+ Rowval; }

}

this.rawValue = All;

However I tried this:

this.rawValue = parent.parent._Kalk.count;

and it worked. (see right below of picture). There are 3 rows and it counts 3.

Then I tried following and it worked (right below corner of pic below):

var i = 1;

var Rowval = xfa.resolveNode("Kalk[" + i + "].Total_einf").rawValue - (10 * xfa.resolveNode("Kalk[" + i + "].Quty_einf").rawValue);

this.rawValue = Rowval;

Does anyone know what I am doing wrong?

Best regards,

Markus

#### Accepted Solutions (1)

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile
_Bruce_Robertson
MVP

25-04-2018

Hi Markus,

Scripts in calculation events can be tricky to debug as you don't always get the exception.  Sometimes it helps to place a button next to the field and put the code in the click event.  Do you have Acrobat installed or just Reader?, you need Acrobat for debugging.  You should also have "When exception is thrown:" set to Break and "Enable interactive console" set.

Anyway, the way I would write your loop is;

var All = 0;

var rows = Tabelle1.resolveNodes("Kalk[*]");

for (i = 0; i < rows.length; i++) {

var row = rows.item(i);

if (!row.Total_einf.isNull && !row.Quty_einf.isNull) {

var Rowval = row.Total_einf.rawValue - (10 * row.Quty_einf.rawValue);

if (Rowval > 0) {

All += Rowval;

}

}

}

this.rawValue = All;

Regards

Bruce

markush32574551
Level 2

Likes

2 likes

Total Posts

21 posts

2 solutions
View profile

markush32574551
Level 2

Likes

2 likes

Total Posts

21 posts

2 solutions
View profile
markush32574551
Level 2

26-04-2018

Hey,

No I wanted to validate the the result of the calculation in the if statement. The Rowval is the claculation

var Rowval = xfa.resolveNode("Kalk[" + i + "].Total_einf").rawValue - (10 * xfa.resolveNode("Kalk[" + i + "].Quty_einf").rawValue);

//I check if overtime was made

if (Rowval > 0) {

I found out that I just forgot to delete the ! operator, my Bad ^^.

Thanks a lot man, it works fine!

Best regards,

Markus

edit: haha, now i understand what you meant, you are right. Sorry man I can hardly focus since I am working on this permantly for the last three days.

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile
_Bruce_Robertson
MVP

26-04-2018

Hi,

I'm not sure I understand, isn't that what the "if (Rowval > 0) {" line was for.  I had the line "if (!row.Total_einf.isNull && !row.Quty_einf.isNull) {" to say skip that row if either field was null, the ! operator in the isNull property meaning that it is not null.

Bruce

markush32574551
Level 2

Likes

2 likes

Total Posts

21 posts

2 solutions
View profile

markush32574551
Level 2

Likes

2 likes

Total Posts

21 posts

2 solutions
View profile
markush32574551
Level 2

26-04-2018

Hey,

Thanks a lot the code works!

Only one question: I need the validation for more then 10 hours.sow i tried for the if statement

if ((!row.Total_einf .rawValue - (10 * !row.Quty_einf.rawValue)) > 2) {

but this doesn't work. Why does it work with .isNull and how can I use the rawValue?

Best regards,

Markus

markush32574551
Level 2

Likes

2 likes

Total Posts

21 posts

2 solutions
View profile

markush32574551
Level 2

Likes

2 likes

Total Posts

21 posts

2 solutions
View profile
markush32574551
Level 2

25-04-2018

Hey,

First of all thanks for your help I appreciate it!

I tried both operators and var All = All + Rowval, both didn't work.

I already enabled it, but didn't find out how to debug it... might be impossible without error code.

I even tried it with "use strict"; at the beginning, but nothing changed.

I might will have to find a workaround since I really don't know how to run it this way.

Best regards,

Markus

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile
_Bruce_Robertson
MVP

25-04-2018

Hi Markus,

Your looping look ok to me, though you can just do "for (i = 0; i <= _Kalk.length ; i++) {", as the form will work up until it resolves the reference.

But the thing that looks wrong to me is the =+ operator, it should be += and I would have thought you would get an error exception thrown.

In Acrobat under Tools ... Preferences ... JavaScript do you have "Show console on errors and messages" ticked?

Regards

Bruce

markush32574551
Level 2

Likes

2 likes

Total Posts

21 posts

2 solutions
View profile

markush32574551
Level 2

Likes

2 likes

Total Posts

21 posts