Skip to main content
January 17, 2018
Answered

Custom Calculation Script Help Please

  • January 17, 2018
  • 1 reply
  • 764 views

Hello,

I have no experience with JavaScript. Can someone please help me with a custom calculation for a checkbook register? 

Example of Field Names used on each line of the form:

Amt1       Dep1     Bal1

Amt2       Dep2     Bal2

I've tried the following Simplified Field Notation:  BalanceForward - Amt1 + Dep1. This works on the first line of the form and then auto populates the balance field on each remaining line of the form. If there are no entries (check or deposit) I don't want to see the balance populated.

I've also tried the following JavaScript with similar results. The below works in the first line (Bal1 field), however when I change the script to reflect different field names it doesn't calculate and returns nothing.

Example of script in Bal1 field:

event.value=Number(this.getField("BalanceForward").value)-Number(this.getField("Amt1").value)+Number(this.getField("Dep1").value);

I think I need an If/Else statement in Bal2, Bal3, Bal4, etc. fields, however I don't know how to write it! If there are no entries in Amt2 or Dep2 then no balance (Bal2 field) should appear.

Thank you!

This topic has been closed for replies.
Correct answer JR Boulay

Hi.

Try this in Bal1 field (not tested):

var total = (this.getField("BalanceForward").value) - (this.getField("Amt1").value) + (this.getField("Dep1").value);

if (total != "NaN") {

    event.value = total;

}

else {event.value = "";}

1 reply

JR Boulay
Community Expert
JR BoulayCommunity ExpertCorrect answer
Community Expert
January 18, 2018

Hi.

Try this in Bal1 field (not tested):

var total = (this.getField("BalanceForward").value) - (this.getField("Amt1").value) + (this.getField("Dep1").value);

if (total != "NaN") {

    event.value = total;

}

else {event.value = "";}

Acrobate du PDF, InDesigner et Photoshopographe
January 18, 2018

JR,

Thanks so much! This is much closer than what I was trying.

This script works on the first line of the form and the beginning balance (BalanceForward field) is auto-populating on all lines of the form. Is it possible to show a zero balance or blank until a check or deposit is entered? 

I copied the script you provided (example below), simply changing the field names for remaining lines. It works, but again ideally I don't want the balance field to populate unless there is an entry made.

var total = (this.getField("Bal1").value) - (this.getField("Amt2").value) + (this.getField("Dep2").value);

if (total != "NaN" || total != Infinity  || total != -Infinity) {

    event.value = total;

}

else {event.value = "";}

Thanks again. I truly appreciate the help.

JR Boulay
Community Expert
Community Expert
January 19, 2018

Try this one:

if ((this.getField("BalanceForward").value != "") && (this.getField("Amt1").value != "") && (this.getField("Dep1").value != "")) {

    var total = (this.getField("BalanceForward").value) - (this.getField("Amt1").value) + (this.getField("Dep1").value); 

    if (total != "NaN") { 

        event.value = total; 

    } 

    else {event.value = "";}

}

Acrobate du PDF, InDesigner et Photoshopographe