Skip to main content
Participating Frequently
March 5, 2025
Answered

Compare a sum field against an entered field and add color if not equal

  • March 5, 2025
  • 1 reply
  • 492 views

I have a form that a user enters an amount to be funded, then enters individual amounts of fundings and fees that it must equal. If the sum of the individual funds and fees does not equal the amount to be funded, I'd like to show the amount to be funded box in red to bring it to their attention.

 

I created a hidden field that sums the individual funds and fees ("TotalFundings").

The field the user enters the amount to be funded is ("AmountFunded")

I have a custom calculation in the ("AmountFunded") as follows:

 

var fund = this.getField("AmountFunded").value;
var total = this.getField("TotalFundings").value;{

if(fund == total)
   event.target.fillColor = color.white;
else if (fund != total)
   event.target.fillColor = color.red;
}

 

The only time the "AmountFunded" field is red is only when I click in the box.  I'd like it to be red the entire time as amounts are entered and change to white when the amounts equal.  

 

 

 

Correct answer PDF Automation Station

When call the field in a custom calculation script that is the field where the calculation resides, use event, not this.getField("fieldName").  Also you don't need else if only else because the result is one or the other (no third option).  While your script worked for as written, I would write it like this:

 

var fund = event.value;
var total = this.getField("TotalFundings").value;

if(fund == total)
   event.target.fillColor = color.white;
else event.target.fillColor = color.red;

 

If it's not working, check the calculation order and make sure TotalFundings is calculated before AmountFunded.

1 reply

PDF Automation Station
Community Expert
Community Expert
March 5, 2025

When call the field in a custom calculation script that is the field where the calculation resides, use event, not this.getField("fieldName").  Also you don't need else if only else because the result is one or the other (no third option).  While your script worked for as written, I would write it like this:

 

var fund = event.value;
var total = this.getField("TotalFundings").value;

if(fund == total)
   event.target.fillColor = color.white;
else event.target.fillColor = color.red;

 

If it's not working, check the calculation order and make sure TotalFundings is calculated before AmountFunded.

Participating Frequently
March 5, 2025

I updated the script to what you have and I made sure the calculations are in the right order.  

The problem is the box isn't showing red unless I click IN the box.  The user will first enter the Amount to be Funded and then proceed with the individual fundings and fees. They won't be clicking back into this box so it needs to appear red while they enter all the other amounts, then turn white when the amounts are equal.

See attached snippets of my document.  For testing, I've made my hidden field that is the sum of the individual fundings and fees as visible so you can see the amounts are not equal. 

PDF Automation Station
Community Expert
Community Expert
March 5, 2025

That's because you have field highlighting turned on.  The blue highlights are on top of the actual colors.  Press Ctrl + k to open the preferences.  In the forms category, unselect this check box: