Skip to main content
Inspiring
August 27, 2025
Answered

Script to calculate text fields

  • August 27, 2025
  • 1 reply
  • 213 views

I've entered the following script in a text fields named 'TotalOTHrs1' and 'TotalRegHrs2', which I need to calculate from text fields 'Hrs1' to 'Hrs8.   I also have text fields named 'TotalOTHrs2' and 'TotalRegHrs2' which I need to calculate from text fields 'Hrs9' to Hrs 16'.  The script below works for an all the fields but I need help figuring out how to include this in my script to make it calculate only the fields I need.  Thank you!

 

//Total OT Hours
var maxFields = 35; //
var total = 0;
for (var i=1; i<=maxFields; i++) {
if (this.getField("Exception"+i).valueAsString!=" ")
if (this.getField("Exception"+i).valueAsString!="Lead Ranger Step 3")
if (this.getField("Exception"+i).valueAsString!="Senior Park Ranger")
if (this.getField("Exception"+i).valueAsString!="Sick")
if (this.getField("Exception"+i).valueAsString!="Sick No Pay")
if (this.getField("Exception"+i).valueAsString!="Leave No Pay")
total+=Number(this.getField("Hrs"+i).valueAsString);
}
event.value = total;

//Total Reg Hours
var maxFields = 35; //
var total = 0;
for (var i=1; i<=maxFields; i++) {
if (this.getField("Exception"+i).valueAsString!="Paid OT1.5")
if (this.getField("Exception"+i).valueAsString!="Bank OT1.5")
if (this.getField("Exception"+i).valueAsString!="Paid OT2.0")
if (this.getField("Exception"+i).valueAsString!="Bank OT2.0")
if (this.getField("Exception"+i).valueAsString!="Meal Break")
total+=Number(this.getField("Hrs"+i).valueAsString);
}
event.value = total;

Correct answer try67

This is not an ideal way of setting up a complex if-statement. Use something like this, instead:

 

for (var i=9; i<=16; i++) {
	var exception = this.getField("Exception"+i).valueAsString;
	if (exception!=" " && exception!="Lead Ranger Step 3" && exception!="Senior Park Ranger" && exception!="Sick" && exception!="Sick No Pay" && exception!="Leave No Pay") {
		total+=Number(this.getField("Hrs"+i).valueAsString);
	}
}

1 reply

Inspiring
August 28, 2025

Figured it out...

 

//Total Reg Hours
var total = 0;var total = 0;
for (var i=9; i<=16; i++) {
if (this.getField("Exception"+i).valueAsString!="Paid OT1.5")
if (this.getField("Exception"+i).valueAsString!="Bank OT1.5")
if (this.getField("Exception"+i).valueAsString!="Paid OT2.0")
if (this.getField("Exception"+i).valueAsString!="Bank OT2.0")
if (this.getField("Exception"+i).valueAsString!="Meal Break")
if (this.getField("Exception"+i).valueAsString!="Callout Paid2.0")
if (this.getField("Exception"+i).valueAsString!="Callout Bank2.0")
total+=Number(this.getField("Hrs"+i).valueAsString);
}
event.value = total;


//Total OT Hours
var total = 0;var total = 0;
for (var i=9; i<=16; i++) {
if (this.getField("Exception"+i).valueAsString!=" ")
if (this.getField("Exception"+i).valueAsString!="Lead Ranger Step 3")
if (this.getField("Exception"+i).valueAsString!="Senior Park Ranger")
if (this.getField("Exception"+i).valueAsString!="Sick")
if (this.getField("Exception"+i).valueAsString!="Sick No Pay")
if (this.getField("Exception"+i).valueAsString!="Leave No Pay")
total+=Number(this.getField("Hrs"+i).valueAsString);
}
event.value = total;

 

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
August 28, 2025

This is not an ideal way of setting up a complex if-statement. Use something like this, instead:

 

for (var i=9; i<=16; i++) {
	var exception = this.getField("Exception"+i).valueAsString;
	if (exception!=" " && exception!="Lead Ranger Step 3" && exception!="Senior Park Ranger" && exception!="Sick" && exception!="Sick No Pay" && exception!="Leave No Pay") {
		total+=Number(this.getField("Hrs"+i).valueAsString);
	}
}
Inspiring
August 29, 2025

Awesome!  Thank you Try!!