Skip to main content
Participant
Feb 18, 2025 4:01 PM UTC
Question

Calculating sum of drop down fields

 


I am trying to find sum of # of contact and time drop down field selected from first picture. There can be multiple selection of same field. And the sum should be added to the second picture.

6 replies

try67
Community Expert
Community Expert
Feb 18, 2025 5:39 PM UTC

So you want to add up the corresponding values under the # of Contacts column for all items with the same value under the Contact Type column, basically? What are the names of the fields in both columns?

Translate
Participant
Feb 19, 2025 3:48 PM UTC

Yeah I used this code for fields # of contacts it's working and giving me sum value in the # of contacts in second page of pdf 

var total = 0;

for (var i=0; i<=12; i++) {

if (this.getField("Select Contact Type1."+i).valueAsString—"Assist Other Agency"){
totalTime+=Number(this.getField (*# Contacts 1."+i).valueAsString);

}
}

event.value = total;

But for the Total time field I used the code 

function timeToSeconds(time) i

var parts = time.split(:);

return (+parts[0] * 3600) + (+parts[1] * 60) + (+parts[2]);

}

function secondsToTime(seconds) D

var hours = Math.floor(seconds / 3600);

var minutes = Math.floor(seconds % 3600) / 60);

seconds = seconds % 60;

return String(hours) padstart(2, 'O') + ' + String(minutes).padStart(2, '0') +': + String(seconds)-padStart(2, '0);
}

var totalTime = 0;

for (var i = 0; i <= 12; i++) |

if (this.getField("Select Contact Type1." + i) valueAsString = "Assist Other Agency") f

var timeString = this.getField("Total Time1," + i).valueAsString:

if (timeString) i totalTime += timeToSeconds(timeString);

}
}

}

event.value = secondsToTime totalTime);

it's not giving me the sum value in second picture Total Time field or any error.

 

Translate
try67
Community Expert
Community Expert
Feb 19, 2025 4:16 PM UTC

You have multiple errors in both codes.

For example, this line:

if (this.getField("Select Contact Type1."+i).valueAsString—"Assist Other Agency"){

Needs to be:

if (this.getField("Select Contact Type1."+i).valueAsString=="Assist Other Agency"){

 

And this line:

if (this.getField("Select Contact Type1." + i).valueAsString = "Assist Other Agency") f

Needs to be:

if (this.getField("Select Contact Type1." + i).valueAsString == "Assist Other Agency") {

 

The code you posted will not be accepted at all, and of course will not work correctly.

Translate