Copy link to clipboard
Copied
Hi, I'm creating the following timesheet.
The LEAVE row is a dropdown menu with Vacation, Personal, Personal Full, and Holiday. I am trying to total them in the correct box to the right. So for example if someone chooses Vacation for Monday, Wednesday, & Friday, it would say 24 in the Vacation box on the right.
I can sum the leave fields: (export value = amount of hours for the selection. For instance Vacation value=8)
var l1 = this.getField("SUNDAYLEAVE1").value;
var l2 = this.getField("MONDAYLEAVE1").value;
var l3 = this.getField("TUESDAYLEAVE1").value;
var l4 = this.getField("WEDNESDAYLEAVE1").value;
var l5 = this.getField("THURSDAYLEAVE1").value;
var l6 = this.getField("FRIDAYLEAVE1").value;
var l7 = this.getField("SATURDAYLEAVE1").value;
event.value = l1+l2+l3+l4+l5+l6+l7
And I can get get the value of a certain choice: (export value = just a different number to define the selection. For instance Vacation export value = 4
var l1 = this.getField("SUNDAYLEAVE2").value;
if(l1 == "4")event.value = 8;
else {
event.value = "";
}
But, I can't figure out how to do both. I'm not sure which approach to purse. Any advice would be appreciated. Thanks
In that case I think you should use "Holiday", "Vacation", etc. as the export values (or have no export values at all) and then hard-code the number of hours for each item into the code, like this:
if (this.getField(fields).valueAsString=="Holiday") total+=8;
Regarding the error messages: The first two are about incorrect field names. The last one is about an undefined variable.
Copy link to clipboard
Copied
So basically if a value was entered in SUNDAYLEAVE2 then you want to ignore the value in SUNDAYLEAVE1, but if no value is entered into it, you want to use the value of the latter in the sum?
Copy link to clipboard
Copied
I don't think I explained this good enough. Sorry. SUNDAYLEAVE1 is the leave field in week 1 and SUNDAYLEAVE2 is in Week 2
It may help if I post a picture in edit mode:
This is what is in the dropdown:
Personal = 4 hours of leave
Personal Full = 8 hours of leave
Vacation = 8 hours of leave
Holiday = 8 house of leave
So if the user in week 1 makes the follow selections:
The form would total
12 in the "PERSONAL" field
32 in the "VACATION" field
8 in the "HOLIDAY" field
Copy link to clipboard
Copied
In that case just multiply the drop-down field with the total field... The user will need to enter "1" under Holiday for it to work, though.
Copy link to clipboard
Copied
Not sure I get what you're suggesting. Let me try this another way...
The "HOLIDAY" field to the far right of the form is the total field for holiday hours for the week.
So the "HOLIDAY" field needs to look at "SUNDAYLEAVE1". If holiday is chosen in the dropdown it would return 8 to the "HOLIDAY" field. If anything else is chosen, it returns nothing to the "HOLIDAY" Field. It then needs to look at the "MONDAYLEAVE1" field and apply the same logic and if necessary add the two results together. Then continue for the other week days.
Copy link to clipboard
Copied
OK, I see. But is "Holiday" the export value, or is "8"?
Copy link to clipboard
Copied
I have tried it both ways and I'm trying to figure out the best approach. Have the export value as 8 certainly makes some other calculations on the form easier.
Copy link to clipboard
Copied
OK, then you can use this code as the custom calculation script of the HOLIDAY field (just fill in the names of the rest of the fields in the first line):
var fields = ["SUNDAYLEAVE1", "MONDAYLEAVE1", ...];
var total=0;
for (var i in fields) {
if (this.getField(fields).valueAsString=="8") total+=8;
}
event.value = total;
Copy link to clipboard
Copied
I'm not getting a result. Can you check my entry?
Am I asking too much to request that you explain your code. I'm trying to learn this stuff and you are using some stuff I haven't worked with.
Copy link to clipboard
Copied
It simply iterates over all the fields in the array, checking their value. If that value is "8" then it adds 8 to the total variable and at the end applies it to the field.
Is it not accepting the code at all? If it does, are there error messages in the JS Console (Ctrl+J) once you change the value of one of the fields?
Copy link to clipboard
Copied
Ok, The problem with that is that Vacation, Personal Full, & Holiday all will have to have an export value of 8. Therefore, it won't just return the HOLIDAY hours.
Here is what's in the Debugger
TypeError: this.getField(...) is null
5:Field:Calculate
TypeError: this.getField(...) is null
4:Field:Calculate
ReferenceError: selectedOption is not defined
2:Field:Calculate
Copy link to clipboard
Copied
In that case I think you should use "Holiday", "Vacation", etc. as the export values (or have no export values at all) and then hard-code the number of hours for each item into the code, like this:
if (this.getField(fields).valueAsString=="Holiday") total+=8;
Regarding the error messages: The first two are about incorrect field names. The last one is about an undefined variable.
Copy link to clipboard
Copied
Ok, I think that is going to do the trick!! I will have to rethink how the form handles the totals without the export values, but I don't think that will be a problem. Thanks so much for your time!!
In reference to the errors... It helps if you can spell Thursday lol.
Copy link to clipboard
Copied
Personally, I always have a problem with Wednesday... 🙂
Copy link to clipboard
Copied
Ha, I have to say it in my head. Wed - Nes - Day.
One more question. On the Personal calculations it could be Personal = 4 hours or Personal Full = 8 hours.
I know I need the follow lines. Would and If/else statement be appropriate for this?
if (this.getField(fields).valueAsString=="Personal") total+=4;
if (this.getField(fields).valueAsString=="Personal Full") total+=8;
Copy link to clipboard
Copied
You can add an "else" before the second line, but it doesn't matter much.