Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Calculate sum based on dropdown selection

Engaged ,
Feb 08, 2021 Feb 08, 2021

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
2.0K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
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;

View solution in original post

Translate
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
Community Expert ,
Feb 08, 2021 Feb 08, 2021

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?

Translate
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
Engaged ,
Feb 08, 2021 Feb 08, 2021

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.

Translate
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
Community Expert ,
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;
Translate
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
Engaged ,
Feb 08, 2021 Feb 08, 2021
LATEST

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

Translate
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