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

Calculate sum based on dropdown selection

Engaged ,
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

1.3K

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

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;

Votes

Translate

Translate
Community Expert ,
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?

Votes

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
Engaged ,
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.

Votes

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
Community Expert ,
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;

Votes

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

Copy link to clipboard

Copied

LATEST

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

Votes

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