Skip to main content
Participant
May 1, 2025
Answered

Sum Total Based on drop down

  • May 1, 2025
  • 3 replies
  • 572 views

I am trying to calculate sum based on a drop down on a page generated from a template , can someone help

 

function doFoodTotal()

{
var aNameParts = event.targetName.split(".");
var cPostFix = aNameParts.pop();
var cPage = aNameParts.shift();


if(/^P\d/.test(cPage))
{
var fSum = this.getField(cPage + ".Additional.Total.").getArray();
var totalF = 0;
for (i = 0; i < fSum.length; i++)
if(this.getField(cPage + ".Additional.Category." + i).valueAsString == "Food")
{
totalF += fSum[i].value;
event.value = totalF;
}
else
event.value = 0;
}
}

Correct answer Tariq Dar

Hi @Vc00


Please wait for more input from experts, try the revised version of your script:

function doFoodTotal() {

    var aNameParts = https://adobe.ly/4jWytfb.split(".");

    var cPostFix = aNameParts.pop();

    var cPage = aNameParts.shift();

 

    if (/^P\d/.test(cPage)) {

        var totalF = 0;

        var i = 0;

 

        while (true) {

            var categoryField = this.getField(cPage + ".Additional.Category." + i);

            var totalField = this.getField(cPage + ".Additional.Total." + i);

 

            if (!categoryField || !totalField) {

                break; // Exit loop when no more fields are found

            }

 

            if (categoryField.valueAsString === "Food") {

                var val = parseFloat(totalField.value);

                if (!isNaN(val)) {

                    totalF += val;

                }

            }

            i++;

        }

 

        event.value = totalF;

    } else {

        event.value = 0;

    }

}

 


~Tariq

3 replies

PDF Automation Station
Community Expert
Community Expert
May 1, 2025

What is the location of the script?  What is the location of the function call?

try67
Community Expert
Community Expert
May 1, 2025

It's hard to help when we don't know what went wrong... Are you getting an error message? If so, what does it say?

Can you share the actual file?

Tariq DarCorrect answer
Legend
May 1, 2025

Hi @Vc00


Please wait for more input from experts, try the revised version of your script:

function doFoodTotal() {

    var aNameParts = https://adobe.ly/4jWytfb.split(".");

    var cPostFix = aNameParts.pop();

    var cPage = aNameParts.shift();

 

    if (/^P\d/.test(cPage)) {

        var totalF = 0;

        var i = 0;

 

        while (true) {

            var categoryField = this.getField(cPage + ".Additional.Category." + i);

            var totalField = this.getField(cPage + ".Additional.Total." + i);

 

            if (!categoryField || !totalField) {

                break; // Exit loop when no more fields are found

            }

 

            if (categoryField.valueAsString === "Food") {

                var val = parseFloat(totalField.value);

                if (!isNaN(val)) {

                    totalF += val;

                }

            }

            i++;

        }

 

        event.value = totalF;

    } else {

        event.value = 0;

    }

}

 


~Tariq

Vc00Author
Participant
May 1, 2025

thank you, this script works great.

Legend
May 1, 2025

@Vc00 - Glad it worked for you. 

 

~Tariq