Expand my Community achievements bar.

SOLVED

Trouble with drop-down list related calculations

Avatar

Level 2

Hello all,

I have been working on a leave form using Adobe LiveCycle Design ES2 for more than three weeks now. I am new to my computer applications programming job, and I have not had much real world experience with Javascript. My problem is this I have two drop-down lists in two separate subforms. I have a separate box for the users to insert the number of hours of leave they want to take next to each drop-down list. At the bottom of the form, I have a subform that I want to track the total number of hours for the leave type the users have selected in the drop-down lists.

I have tried multiple methods from JavaScript to FormCalc, but I have been unsuccessful at finding a way to instruct each of my leave category totals fields to reflect the value in the hours fields (that is...if the leave type list specifies its leave type). I am exhausted of scouring the Internet and books to see if I can find a viable solution to this matter. I have another issue to discuss with this same form in regard to the calculations of my add instances, but it'll have to wait until I am able to get this matter rectified. Can anyone help me?

Leave_Slip rough.png

1 Accepted Solution

Avatar

Correct answer by
Level 7

The script needed for each total field calculation.

The only thing different between them is the "6" is changed for each category (i.e. personal leave is "0"). Select FormCalc in script editor. This definitely works in your form--guaranteed!.

    var oDays    = _IndvSubForm.count

    var totHrs    = 0

    for i = 1 upto oDays  do

        if (IndvSubForm[i-1].DataDropDownList2 == "6")then

            totHrs = totHrs + IndvSubForm[i-1].TotalHours2

            continue

        else

            continue

        endif

    endfor

    $ = totHrs

good luck

stephen

View solution in original post

14 Replies

Avatar

Level 7

Hi,

I am assuming that the the AddDay button adds an instance of a subForm--lets call that subFormDay, OK?

I am also assuming that your dropDownCategory has some sort of paired value in the Binding Tab of the Object Pallet--lets say they are numbered like 

PLD is "1"

MilitaryLeave is "2"

Leave w/o Pay is "3"

etc.

A solution using FormCalc would be to put a script on the calculation event of each category field at the bottom like this (this one would be for PLD cause we're calling PLD "1"), OK?:

var totalHrs = 0                                                                     //Step 0

var oDays = _subFormDay.count                                               //Step 1

for i=1 upto oDays do                                                                 //Step 2

     if(subFormDays[i].dropDownCategory == "1")then                //Step 3

          totalHrs = totalHrs + subFormDays[i].hoursField  //Step 4

          continue                                                                             //Step 5

     endif

endfor

$ = totalHrs                                                                                //Step 6   

To summerize:

Step 0//create a variable to hold the PLD hours and set to 0

Step 1//create a variable, determine the number of subFormDays there are, set the variable to the number of days

Step 2//starting at the first day and ending with the last day

Step 3//see if the category for the day matches the PLD ("1") we're adding up

Step 4//if it does, add it to the PLD total

Step 5//go to the next day, repeat until all days are looked at

Step 6//put the total in the field

With just a little tweaking using your correct object names, this should work. You'll need to put one of these each category total field. Let's say MilitaryLeave is "2", OK,. So the script you place in the MLTotalField will only differ at Step 3:

     if(subFormDays[i].dropDownCategory == "2")then

Cheers!

Stephen

Avatar

Level 2

Hey Stephen,

I do thank you for your help on this predicament thus far. Something you mentioned that was extremely helpful to a livecycle novice like myself was your assumptions about the paired value in the Binding Tab. I was previously trying to enter the selection options I had in the drop-down lists, but that caused some more problems for me. After using the binded values (which were already defined when I looked), my values began to change when the appropriate selections were made.

In your suggestion, I believe this portion in step 4 totalHrs = totalHrs + subFormDays[i].hoursFieldwas somehow causing my values to square themselves in the total hours section of this form. I ended up changing it around a little to reflect this: totalHrs = sum(IndvSubForm[*].TotalHours2). That seems to have fixed the squaring phenomenon. I want to show you what I have in my current script, and then I'll tell you what my current problem is.

 

LeaveForm.MainSubForm.LeaveCatSubForm.DisabilityLeave::calculate - (FormCalc, both)

var totalHrs = 0

 

var

oDays = IndvSubForm.TotalHours2

for i=1 upto oDays do

   if(IndvSubForm[*].DataDropDownList2[*]=="5")then  //5 is the value of the Disability Leave selection

      totalHrs = sum(IndvSubForm[*].TotalHours2)

      continue

endfor

   endif

$ = totalHrs

The problem I am experiencing now is that the first choice of the drop-down list is what the calculations are basing the choice on. For instance, when I type a number for the Total Hours nothing happens. I then select Disability Leave. The value is then added to the correct box for the totals calculation. When I click the Add button, the value I type into the newly added Total Hours field is automatically added to the totals calculation--this is done regardless of what I select from the drop-down list. What am I doing wrong now?

Gabriel

Avatar

Level 7

Hi Gabriel,

#1 in the "for" loop, you should be referencing [i] not [*]

#2 I made a mistake, it should be IndvSubForm[i-1] in the loop (not [i]) since the first subform is IndvSubForm[0]  //my bad

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

var oDays = _IndvSubForm.count    //must have underscore and must have .count 

var totalHrs = 0     //must set new var to 0

for i=1 upto oDays do

   if(IndvSubForm[i-1].DataDropDownList2=="5")then 

      totalHrs = totalHrs  + IndvSubForm[i].TotalHours2

      continue

     endif                         //put endif here before endfor

endfor

$ = totalHrs

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

If you were adding a field of total hours (not by category) you could use simply 1 line of code in the calculation event of the grand total field, :

$ = Sum(IndvSubForm[*].TotalHours2)        //no "for" loop and no "if" statement needed

but this won't be by category, just a total of all hours in all the TotalHours2 fields

good luck,

Stephen

Message was edited by: kingphysh 

Avatar

Level 2

Hi Stephen,

I made the changes you suggested, and I have stopped getting the error on launching the preview. The problem I am experiencing now is that I get an error when I click the drop-down list. I get this error ("accessor 'IndvSubForm[1].TotalHours2" is unknown). I get the same error message for every instance except the second instance. It will add the oncoming values to the total, but it disregards the drop-down list selections. It is doing some other unexpected things as well. I imagine once one issue is fixed, the others will fall in line. Here is the script I have now:

Leave_Slip rough2.png

Avatar

Level 7

Hi,

The script can't find the subform--probably the name is wrong. Maybe use:

LeaveCatSubForm[i-1].DataDropDownList2 

There are various ways to determine the correct SOM reference. At the top of the script editor (look in the image you posted at the top) you'll see the SOM fof the object you are editing followed by ::   Go to the object you want to reference and by looking in the script editor you will see its SOM reference.

You only need the relative SOM

Another way is to place the cursor in the script editor, press Ctr and click on the object in the design view.It will write the correct relative SOM reference into the script editor for you. Then insert the [i-1] where the subform that repeats is.

Good luck!

Stephen

Avatar

Level 2

Thanks Stephen,

To say that you’re very helpful is a horrible understatement. I’ll give that a try.

Gabriel

Avatar

Level 2

Stephen,

I am going to attach the file to this message. I don’t know if you will be able to get a hold of it, but if you can it will definitely help take the guess work out of things on your end. In the case you cannot, I can try to explain what I am noticing now.

(1) The name of the sub form is correct at IndvSubForm.

(2) I notice that I receive that particular error message only occurs when I select the leave category from the drop-down list. It is always looking for the next instance of the sub form. For example the first instance of IndvSubForm is Re: Trouble with drop-down list related calculations (which is IndvSubForm[0]). When I select disability leave from the drop-down list, I run into opposition. I get an error message saying, “Error: accessor ‘IndvSubForm[1].TotalHours2’ is unknown.” When I add the new instance of the sub form and select disability from the drop-down list, it tells me “IndvSubForm[2].TotalHours2  is unknown.” For instance 3 it says 4 is unknown and so forth. These errors only occur when I select Disability Leave from the drop list. It is the #5 value I have instructed the script to look for in the field LeaveCatSubForm.DisabilityLeave.

(3) The only field the script seems concerned with when listening for the value of the drop-down list is the second instance of the IndvSubForm. If that field is set to disability leave, then it will add the total of each proceeding value regardless of drop-down list selection. It will not look at the first instance (which is IndvSubForm[0]).

I hope I have been able to be descriptive enough. Again, I want to express my extreme thanks of all that you have done thus far, and I hope you will stick through this one with me to the completion. Here is a copy of what I have this far:

Gabriel

var oDays = _IndvSubForm.count

var totalHrs = 0

for i=1 upto oDays do

  if(IndvSubForm[i-1].DataDropDownList2=="5")then

    totalHrs = totalHrs + IndvSubForm[i].TotalHours2

    continue

  endif

endfor

$=totalHrs

Avatar

Level 7

Gabriel,

Please open your Hiearchy Pallet and click all the + so everything is visible. Try to expose as much as you can--open it wider if it helps. Then take a screen shot of it and post it here.

Stephen

Avatar

Level 2

Here is the screenshot of the Hiearchy Pallet. The only thing cut from this one due to the lack of screen height is the name of the form at the very top of the Hierachy Pallet.

Leave Form.png

Avatar

Level 7

Looks like there are 2 subForms that have hours that need to go into the total (Consecutive and Individual)--unless you are only doing one of those.

When you add a day, what subform(s) get added? The IndvSubForm? or ConsecSubForm? or both?

If both, wrap those 2 Subforms in another Subform and have that subform be the repeating one where you add and remove instances, OK

In Hiearchy Pallet click the - next to those subforms, highlight both, right click on highlighted subforms and select "wrap in subform.

Name it, set the binding to "Repeat for each data item", and change the scripts on your buttons to add and remove that subform. Then the scripts in the total fields will need to be changed.

count the new subforms

add another line in the loop to add the 2nd total hours field to the total

loop

     total = total + one

     total = total + the other one

    

hopefully you get the idea.

Busy day, week as usual--try and make it work--I won't be available the rest of the day.

Good luck!

Avatar

Level 2

Stephen,

You are correct in that there are 2 sub forms that have hours needing to be added in the total hours section. My biggest problem has been the repeating individual days sub form. I figured I would go back and script the addition for the calculation of the two sub forms (with a potential of multiple instances) after getting the bugs worked out of the new instance matter. The Consecutive sub form is not repeating, so I didn’t wrap it with the individual hours sub form. I understand that you’re busy and will not be able to respond today. This is another attempt at putting this file here in a way that you can view it. Thanks.

Gabriel

Leave Application_2 - Copy.pdf

Avatar

Correct answer by
Level 7

The script needed for each total field calculation.

The only thing different between them is the "6" is changed for each category (i.e. personal leave is "0"). Select FormCalc in script editor. This definitely works in your form--guaranteed!.

    var oDays    = _IndvSubForm.count

    var totHrs    = 0

    for i = 1 upto oDays  do

        if (IndvSubForm[i-1].DataDropDownList2 == "6")then

            totHrs = totHrs + IndvSubForm[i-1].TotalHours2

            continue

        else

            continue

        endif

    endfor

    $ = totHrs

good luck

stephen

Avatar

Level 2

Sorry for not responding sooner Stephen,

I’ve been out for the holidays. I’ll get on this today and let you know how it works out. I hope you had a festive Christmas. I know my family and I did.

Gabriel

Avatar

Level 2

Stephen,

Success!! Thanks tremendously for sticking with me throughout this process! I would assign you more points if I could because I know this was not the easiest of fixes for you. I have been able to finish up this form thanks—in no short part—to your input. Here is the completed form:

Completed Leave Application_Copy.pdf

Take care. God Bless you and your family and Happy New Year!