Skip to main content
Participant
January 4, 2019
Answered

getting the minimum field only calculating fields with values

  • January 4, 2019
  • 3 replies
  • 1267 views

Hi

My fields are "Legal Rent" & "Pref Rent"

I want my new field "Rent Amount" to take the minimum value of the two above fields

However, if the field "Pref Rent" is blank as is the case many times I only want the new field to populate the value in "Legal Rent" and not to take the minimum in that case which is "0"

I am not so familiar with Java and I have tried pasting calculations I have found but have not been able to get anything to work for this one

This topic has been closed for replies.
Correct answer arigoldberg

This worked the first time I put it in but then I changed the numbers in the fields "Legal Rent" and "Pref Rent" and although it still shows The value from "Legal Rent" when it is the only value, when I pur any number into "Pref Rent" this new field says "NAN"

3 replies

Inspiring
January 4, 2019

You have told us what to set the Rent Amount to if Legal Rent is zero or null and there is an amount for Pref Rent. Assuming that Legal Rent need to be greater than zero, the custom JavaScript calculation for the Rent amount could be:

var LegalRent = this.getField("Legal Rent").value;

var PrefRent = this.getField("Pref Rent").value;

event.value = ""; // clear Rent Amount;

if(PrefRent == 0){

PrefRent = LegalRent; // set Prref Rent to Legal Rent value;

}

if(LegalRent != 0) {

event.value = Math.min(LegalRent, PrefRent); // select minimum of Legal Rent or Pref Rent;

}

try67
Community Expert
Community Expert
January 4, 2019

You can use this code as the custom calculation script of your field:

var v1 = this.getField("Pref Rent").valueAsString;

var v2 = this.getField("Legal Rent").valueAsString;

if (v1=="") event.value = v2;

else event.value = Math.min(Number(v1), Number(v2));

arigoldbergAuthorCorrect answer
Participant
January 9, 2019

This worked the first time I put it in but then I changed the numbers in the fields "Legal Rent" and "Pref Rent" and although it still shows The value from "Legal Rent" when it is the only value, when I pur any number into "Pref Rent" this new field says "NAN"

Inspiring
January 9, 2019

Do you have the fields' format set to numeric and are you entering the values without the thousands separator?

Bernd Alheit
Community Expert
Community Expert
January 4, 2019

You must use Javascript, not Java.