Calculate sum based on dropdown selection

Contributor ,
Feb 08, 2021 Feb 08, 2021

Copy link to clipboard

Copied

I've added a calculation script to 'CDOHrs' text field to add the total sum when CDO Banked is selected from the Absence drop down selection.  However, I have another 'TotalHrs' text field which I would like to have all other hours calculated, except when CDO Banked is selected from the dropdown list.  But, I also need the hours to calculate if no selection has been entered for Absence Type.  I'm stuck on how to do that part.  Any help would be greatly appreciated.  I've attached a document as an example.  I hope this makes sense.  Thank you!!

TOPICS
Create PDFs, JavaScript, PDF forms

Views

96

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Most Valuable Participant , Feb 08, 2021 Feb 08, 2021
Assuming the fields all follow the same name pattern you can use this code as the custom calculation script of the TotalHrs field, then: var maxFields = 2; // adjust this value to 45 in the full version var total = 0; for (var i=1; i<=maxFields; i++) { if (this.getField("AbsencePayTypeDesc"+i).valueAsString!="CDO Banked (worked on CDO)") total+=Number(this.getField("RegHrs"+i).valueAsString); } event.value = total;

Likes

Translate

Translate
Most Valuable Participant ,
Feb 08, 2021 Feb 08, 2021

Copy link to clipboard

Copied

So basically you want Total Hrs to be RegHrs1 + RegHrs2, unless the "CDO Banked (worked on CDO)" option is selected in their corresponding Absence Type drop-downs?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 08, 2021 Feb 08, 2021

Copy link to clipboard

Copied

Ya, I actually have up to RegHrs45 on my form, but basically that's what I need.  It will help the user to have the total hours calculated for CDO Banked in a seperate field, and all other hours in the Total Hours text field.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Feb 08, 2021 Feb 08, 2021

Copy link to clipboard

Copied

Assuming the fields all follow the same name pattern you can use this code as the custom calculation script of the TotalHrs field, then:

 

var maxFields = 2; // adjust this value to 45 in the full version
var total = 0;
for (var i=1; i<=maxFields; i++) {
	if (this.getField("AbsencePayTypeDesc"+i).valueAsString!="CDO Banked (worked on CDO)")
		total+=Number(this.getField("RegHrs"+i).valueAsString);
}
event.value = total;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 08, 2021 Feb 08, 2021

Copy link to clipboard

Copied

LATEST

Fantastic!  That works beautiful!!  Thank you so much!!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines