Skip to main content
Participant
Feb 19, 2025 3:03 AM UTC
Answered

Field equals lesser of two values; one field sum of multiples, other is value

I'm new to this java business... 

I need help defaulting to the amount in field A if the true total of fields b, c, d, e, etc (TrueTotal) is greater than A.

 

if A = TrueTotal value = truetotal

if A <TrueTotal, then default to A, notify user that amount doesn't match 

if A>TrueTotal, then TrueTotal, notify user that amounts don't match and A is too high

 

Thanks so much.

Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
Feb 19, 2025 12:31 PM UTC

So you just want to show messages?

This will show messages when all fields are filled, place it as custom calculation script of any text field:

var fields = ["A", "B", "C", "D", "E"];
var values = fields.map(name => this.getField(name).valueAsString);

if (event.source && fields.indexOf(event.source.name) !== -1) {
 if (values.every(v => v !== "")) {
  var [a, b, c, d, e] = values.map(Number);
  var trueTotal = b + c + d + e;

  if (a < trueTotal) {
   app.alert("Warning: The amount entered in field A is lower than the total calculated amount.", 3);} 
  else if (a > trueTotal) {
   app.alert("Warning: The amount in field A is too high and does not match the calculated total.", 3);}}}

5 replies

Participant
Feb 19, 2025 11:50 AM UTC

Wow. So fast! Thank you. 

The value of A is static and the true total is sum of other values. I need to show the messages. I can have a hidden field for actual if that helps

Translate
Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
Feb 19, 2025 12:31 PM UTC

So you just want to show messages?

This will show messages when all fields are filled, place it as custom calculation script of any text field:

var fields = ["A", "B", "C", "D", "E"];
var values = fields.map(name => this.getField(name).valueAsString);

if (event.source && fields.indexOf(event.source.name) !== -1) {
 if (values.every(v => v !== "")) {
  var [a, b, c, d, e] = values.map(Number);
  var trueTotal = b + c + d + e;

  if (a < trueTotal) {
   app.alert("Warning: The amount entered in field A is lower than the total calculated amount.", 3);} 
  else if (a > trueTotal) {
   app.alert("Warning: The amount in field A is too high and does not match the calculated total.", 3);}}}
Translate
Participant
Feb 27, 2025 3:15 PM UTC

Thanks this is great. 

Translate
Nesa Nurani
Community Expert
Community Expert
Feb 19, 2025 4:36 AM UTC

Do you wish to show the result in "A" field or another field?
Should all fields be filled before checking?

Translate