Copy link to clipboard
Copied
I have five input fields (amount.1 through amount.5) that sum up in a Total field. In the Total field, I have a script (see below) in Validate that compares to another field (Adjustment amount) and alerts the user if Total exceeds Adjustment amount. The alert works fine, but I would like for it to clear or zero out the last field that was input that triggered the alert.
var TAA = this.getField("txtAdjustmentAmt").value;
if (TAA > 0) { //If TAA returns with a negative number, then the following will not occur
if (event.value > TAA){
app.alert("Total to apply cannot exceed the Adjustment amount")
event.rc = false;
}
}
Copy link to clipboard
Copied
You can't use the value of another field for the total calculation, because that won't update until the new value is validated. Instead, you need to calculate the total in your code (making sure to use the new value of the validated field, instead of the old one). Something like this:
function validateAmount() {
var TTL = 0;
for (var i=1; i<=5; i++) {
var fname = "amount."+i;
if (event.target.name==fname) TTL+=Number(event.value);
else TTL+=Number(this.getField(fname).valueAsString);
}
var TAA = this.getField("txtAdjustmentAmt").value;
if (TAA > 0) { //If TAA returns with a negative number, then the following will not occur
if (TTL > TAA){
app.alert("Total to apply cannot exceed the Adjustment amount")
event.rc = false;
}
}
}
Then call validateAmount() from the Validation event of each of the amount.x fields.
However, note that this will not work if you change the value of txtAdjustmentAmt once the amount fields have been filled in. I would recommend resetting those fields when that happens, to make sure the values are valid.
Copy link to clipboard
Copied
In a custom calculation, the field that triggered the calculation is provided in the event object.
in "event.source". But of course this could be any field on the form.
Here's the reference entry:
One solution is to capture and save the value in the calculation script so it can be used later in the validation script.
Another solution is to put all the code into the calculation script.
For this to work the calculation will need to be setup to filter out any sources that are not from the "amount" fields.
Copy link to clipboard
Copied
You should move the code to the Validation script of each one of the amount fields. Then you would be able to reject the latest value entered, if it exceeds the allowed total.
Copy link to clipboard
Copied
Hello Try67,
I tried as you suggested, but the alert message does not appear until a calculation (step) after the Total has exceeded the Adjustment amount. Below is the code used in each of the Amount fields:
var TAA = this.getField("txtAdjustmentAmt").value;
var TTL = this.getField("txtTtlToApply").value;
if (TAA > 0) { //If TAA returns with a negative number, then the following will not occur
if (TTL > TAA){
app.alert("Total to apply cannot exceed the Adjustment amount")
event.rc = false;
}
}
Copy link to clipboard
Copied
You can't use the value of another field for the total calculation, because that won't update until the new value is validated. Instead, you need to calculate the total in your code (making sure to use the new value of the validated field, instead of the old one). Something like this:
function validateAmount() {
var TTL = 0;
for (var i=1; i<=5; i++) {
var fname = "amount."+i;
if (event.target.name==fname) TTL+=Number(event.value);
else TTL+=Number(this.getField(fname).valueAsString);
}
var TAA = this.getField("txtAdjustmentAmt").value;
if (TAA > 0) { //If TAA returns with a negative number, then the following will not occur
if (TTL > TAA){
app.alert("Total to apply cannot exceed the Adjustment amount")
event.rc = false;
}
}
}
Then call validateAmount() from the Validation event of each of the amount.x fields.
However, note that this will not work if you change the value of txtAdjustmentAmt once the amount fields have been filled in. I would recommend resetting those fields when that happens, to make sure the values are valid.