Skip to main content
Participating Frequently
September 13, 2024
Answered

Subtract currency formatted fields for running total

  • September 13, 2024
  • 2 replies
  • 499 views

I need to subtract a series of currency fields to get a running total as they are being filled in. I have tried both a simplified fields notation and custom calculation script and I still can't get anything to show up in the running total field. Screenshots attached. Scripts below. Any guidance appreciated, clearly I'm no JavaScript expert. Thanks!

 

Simplified (I have tried this as a long series of subtractions and the way you see it below):

Total Cost - (FederalGrants + ScholarshipAmount1 + ScholarshipAmount2 + ScholarshipAmount3 + Subsidized + Unsubsidized + PLUS + VA + Agency + AlternativeLoans + Payments)

 

Custom:

event.value = this.getField("Total Cost").value - this.getField("FederalGrants").value - this.getField("ScholarshipAmount1").value - this.getField("ScholarshipAmount2").value - this.getField("ScholarshipAmount3").value - this.getField("Subsidized").value - this.getField("Unsubsidized").value - this.getField("PLUS").value - this.getField("VA").value - this.getField("Agency").value, this.getField("AlternativeLoans").value - this.getField("Payments").value - result;

 

This topic has been closed for replies.
Correct answer Nesa Nurani

Try like this:

var totalCost = Number(this.getField("Total Cost").valueAsString);
var federalGrants = Number(this.getField("FederalGrants").valueAsString);
var scholarship1 = Number(this.getField("ScholarshipAmount1").valueAsString);
var scholarship2 = Number(this.getField("ScholarshipAmount2").valueAsString);
var scholarship3 = Number(this.getField("ScholarshipAmount3").valueAsString);
var subsidized = Number(this.getField("Subsidized").valueAsString);
var unsubsidized = Number(this.getField("Unsubsidized").valueAsString);
var plus = Number(this.getField("PLUS").valueAsString);
var va = Number(this.getField("VA").valueAsString);
var agency = Number(this.getField("Agency").valueAsString);
var alternativeLoans = Number(this.getField("AlternativeLoans").valueAsString);
var payments = Number(this.getField("Payments").valueAsString);


event.value = totalCost - federalGrants - scholarship1 - scholarship2 - scholarship3
              - subsidized - unsubsidized - plus - va - agency
              - alternativeLoans - payments;

2 replies

Nesa Nurani
Nesa NuraniCorrect answer
Adobe Expert
September 14, 2024

Try like this:

var totalCost = Number(this.getField("Total Cost").valueAsString);
var federalGrants = Number(this.getField("FederalGrants").valueAsString);
var scholarship1 = Number(this.getField("ScholarshipAmount1").valueAsString);
var scholarship2 = Number(this.getField("ScholarshipAmount2").valueAsString);
var scholarship3 = Number(this.getField("ScholarshipAmount3").valueAsString);
var subsidized = Number(this.getField("Subsidized").valueAsString);
var unsubsidized = Number(this.getField("Unsubsidized").valueAsString);
var plus = Number(this.getField("PLUS").valueAsString);
var va = Number(this.getField("VA").valueAsString);
var agency = Number(this.getField("Agency").valueAsString);
var alternativeLoans = Number(this.getField("AlternativeLoans").valueAsString);
var payments = Number(this.getField("Payments").valueAsString);


event.value = totalCost - federalGrants - scholarship1 - scholarship2 - scholarship3
              - subsidized - unsubsidized - plus - va - agency
              - alternativeLoans - payments;
Participating Frequently
September 16, 2024

@Nesa Nurani that worked perfectly! Thank you for helping me on this. I will make note of it for the future. I appreciate it!

PDF Automation Station
Adobe Expert
September 14, 2024

You should check the console for errors as you most likely have errors in your script.  With the simplified field notation, you can't have spaces in your field names (Total Cost).  With the custom script, you didn't define result and you have a comma after this.getField("Agency").value instead of a subtraction sign.

Participating Frequently
September 16, 2024

@PDF Automation Station thank you for the input. I will keep it in mind going forward.