Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Adding values from mulitple arrays in a for loop with if then statement

Avatar

Level 3

So I have a table set up and need to add up the hours for specific activities.

I have Activity() as an array of drop down boxes with things like "holiday, regular, vacation, etc"

Monday(), Tuesday() etc, are numeric field arrays.

and i am trying to popluation totals from the differnet Activities.

Using Java like this and it doesn't seem to be working:

for (var i = 0; i < 12; i++) 

{

    if (Activity[i].rawValue = "Holiday"){

    $.Value = Monday[i].value + Tuesday[i].value + Wednesday[i].value + Thursday[i].value + Friday[i].value

    }

    }

Am i missing somethign here?

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi,

Use FormCalc (not JavaScript). I think you have a "total" field for each week (i.e. total and Monday are siblings?). If so

//////////////////////////////////////////////////////////////////////

     if   (Activity == "Holiday")then

          $ = Sum(Monday, Tuesday, Wednesday, Thursday, Friday)

     else

          $ = 0

     endif

//////////////////////////////////////////////////////////////////////

If you are totalling for multiple weeks (all the weeks),

     if   (Activity == "Holiday")then

          $ = Sum(week[*]Monday, week[*]Tuesday, week[*]Wednesday, week[*].Thursday, week[*].Friday)

     else

          $ = 0

     endif

//where each group of days is wrapped in a subform named "week"

//////////////////////////////////////////////////////////////////////

Finally, this uses the "boundItem" values NOT the displayed value. So:

     if   (Activity == "Holiday")then

will only work if the boundItem value is used. Look in the BindingTab for the dropdown box to understand and setup some boundItem values. They are strings and use "" when referencing strings. So, 1 is best referenced as "1" as in

if   (Activity == "1")then

//////////////////////////////////////////////////////////////////////

Finally, if statements need == (don't use a single =).

Hope this helps!

Stephen

View solution in original post

10 Replies

Avatar

Level 7

Hi,

  • Need more information.
  • On what field and what event are you placing your script?
  • $ in doesn't work in JavaScript (use "this")
  • If you are placing the script on a dropdown box, on the Change event, "this" returns the previous value, not the current value
  • if Monday[i].value is referencing a SOM object (field or dropdown box, etc),  you need rawValue instead of .value
  • If you are referencing SOM objects (fields, etc.) using Monday[i].rawValue in JavaScript, you will need to use the resolveNode() method and build a string like the following:

         this.resolveNode( "Monday[" + i + "]").rawValue;       //there are 3 strings concatonated using + within the ()

  • All javaScript statements must have a semicolon; at the end as my example above

Hope this helps

Stephen

Avatar

Level 3

The script is on the Calcuate event of the the "total" field so as the add hours on differnet days the total will change.

So what i am trying to do is run through the different activity fields and find with column is "Holiday" if it happens to be Activity[5].rawValue = "Holiday" then I want the totals for Monday[5].rawValue, tuesday, wednesday etc.

Avatar

Level 3

So i switched to FormCalc and got this to work

for I=0 upto Activity.nodes.length - 1 step 2 do

if (Activity[I] eq "Holiday") then

HolidayHours.rawValue = Monday[I] + Tuesday[I] etc...

endif

endfor

Probelm is I can't figure out in what even to place it so it is updated everytime the user edits any of the hours.

Avatar

Correct answer by
Level 7

Hi,

Use FormCalc (not JavaScript). I think you have a "total" field for each week (i.e. total and Monday are siblings?). If so

//////////////////////////////////////////////////////////////////////

     if   (Activity == "Holiday")then

          $ = Sum(Monday, Tuesday, Wednesday, Thursday, Friday)

     else

          $ = 0

     endif

//////////////////////////////////////////////////////////////////////

If you are totalling for multiple weeks (all the weeks),

     if   (Activity == "Holiday")then

          $ = Sum(week[*]Monday, week[*]Tuesday, week[*]Wednesday, week[*].Thursday, week[*].Friday)

     else

          $ = 0

     endif

//where each group of days is wrapped in a subform named "week"

//////////////////////////////////////////////////////////////////////

Finally, this uses the "boundItem" values NOT the displayed value. So:

     if   (Activity == "Holiday")then

will only work if the boundItem value is used. Look in the BindingTab for the dropdown box to understand and setup some boundItem values. They are strings and use "" when referencing strings. So, 1 is best referenced as "1" as in

if   (Activity == "1")then

//////////////////////////////////////////////////////////////////////

Finally, if statements need == (don't use a single =).

Hope this helps!

Stephen

Avatar

Level 3

Thanks for your patience. Working pretty good now.

Avatar

Level 3

So I have a Total Field that on the calculate event i have:

Sum(Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday)

However when i add your if statement:

if   (Activity == "Holiday")then

          Holiday.rawValue = Sum(Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday)

     else

          Holiday.rawValue = 0

endif

The total field no longer totals anything UNLESS I choose holiday for the Activity field.... Any idea why that is?

Avatar

Level 7

Hi,

I made the assumption that was what you wanted. So the

     else

          $ = 0

part of the script does that. What is it you want it to do instead?

Stephen

Avatar

Level 3

Oh sorry yes. I guess i need to go get lunch.

I have a "Total" column and then a a break out section of what the the hours are as monday can have 3 hours of Vacation and 5 hours of regular, so i just had to tweak the if statement and yes replace the $=0 with what i wanted.

Avatar

Level 7

Hi,

I'm still not sure of how your form is supposed to work.

I say                     $ = 0                              //which means "this" equals zero

but you say           Holiday.rawValue = 0   //which means "Holiday" equals zero

Is Holiday the field that the script is on or some other field?

Avatar

Level 3

Yes. There is the "Total" field which gets the $=0 but I added "holiday" to total just holiday hours. So I took your formula and added a bunch elseif statements.

I can post my final formula later as now I am out of the office.

Best Regards,

Benjamin Speich

Customer Service Specialist

Mentice Inc.

Cell 312-504-8820