8 Replies Latest reply on Sep 16, 2013 1:28 PM by petermarksATL

I have so little experience with this it isn't funny.  I prior to now have never used javascript.  But Im trying to get my lease form to auto calculate a tenants pro-rate that would be due if they moved in on the middle of the month for example.

I created the below script and im sure there is a few things or more than a few things wrong with it...can anyone give me some pointers.

// Custom Calculate script

(function () {

// Get the input field values, as strings

var leasemonth = getField("LeaseBegMonth").valueAsString;

var leasestartday = getField("LeaseBegDay").valueAsString;
var rentamount = getField("MonthlyRent").valueAsString;

// calculate how many days in the month

if ( leasemonth=1
var leasedays = 31
)
elseif (leasemonth=2
var leasedays = 28
)
elseif (leasemonth=3
var leasedays = 31
)
elseif (leasemonth=4
var leasedays = 30
)
elseif (leasemonth=5
var leasedays = 31
)
elseif (leasemonth=6
var leasedays = 30
)
elseif (leasemonth=6
var leasedays = 30
)
elseif (leasemonth=7
var leasedays = 31
)
elseif (leasemonth=8
var leasedays = 31
)
elseif (leasemonth=9
var leasedays = 30
)
elseif (leasemonth=10
var leasedays = 31
)
elseif (leasemonth=11
var leasedays = 30
)
elseif (leasemonth=12
var leasedays = 31
)

//CALCULATE HOW MANY DAYS REMAIN
var daysremain = LeaseBegDay-leasedays

//CALCULATE THE PRORATE AND DISPLAY IT
event.value = (rentamount/leasedays)* daysremain

})();

Thanks for your help!!

• ###### 1. Re: Custom calculation script for a lease (please help)

Your if-statements are all wrong... They need to look like this:

var leasedays = 0;

if ( leasemonth==1)

leasedays = 31;

else if (leasemonth==2)

leasedays = 28;

etc.

• ###### 2. Re: Custom calculation script for a lease (please help)

Working on correcting that right now.  Thank you!  Anything else you see wrong?

• ###### 3. Re: Custom calculation script for a lease (please help)

Yes. I just noticed now that all of your variables are strings (since you're using the valueAsString property).
You should convert them to numbers, like so:

var leasemonth = Number(this.getField("LeaseBegMonth").valueAsString);

var leasestartday = Number(this.getField("LeaseBegDay").valueAsString);
var rentamount = Number(this.getField("MonthlyRent").valueAsString);

• ###### 4. Re: Custom calculation script for a lease (please help)

Yes. In this line of code you're accessing the wrong variable name:

var daysremain = LeaseBegDay-leasedays

It should be:

var daysremain = leasestartday-leasedays

• ###### 5. Re: Custom calculation script for a lease (please help)

Thank you thank you thank you!  Here is the revised script with the changes.  about to go plug it in and see if it works.

// Custom Calculate script

(function () {

// Get the input field values

var leasemonth = Number(this.getField("LeaseBegMonth").valueAsString);
var leasestartday = Number(this.getField("LeaseBegDay").valueAsString);
var rentamount = Number(this.getField("MonthlyRent").valueAsString);
var leasedays = 0

// calculate how many days in the month

if ( leasemonth==1)

leasedays = 31;

else if (leasemonth==2)

leasedays = 28;

else if ( leasemonth==3)

leasedays = 31;

else if (leasemonth==4)

leasedays = 30;

else if ( leasemonth==5)

leasedays = 31;

else if (leasemonth==6)

leasedays = 30;

else if ( leasemonth==7)

leasedays = 31;

else if (leasemonth==8)

leasedays = 31;

else if ( leasemonth==9)

leasedays = 30;

else if (leasemonth==10)

leasedays = 31;

else if ( leasemonth==11)

leasedays = 30;

else if (leasemonth==12)

leasedays = 31;

//CALCULATE HOW MANY DAYS REMAIN
var daysremain = leasestartday-leasedays

//CALCULATE THE PRORATE AND DISPLAY IT
event.value = (rentamount/leasedays)* daysremain

})();

• ###### 6. Re: Custom calculation script for a lease (please help)

I think it works!!  Simply amazing...seriously never expected such great help within minutes of posting.

one final question. the amount it displays is extremely long after the decimal point...how do I get it to round to only the hundreths?

• ###### 7. Re: Custom calculation script for a lease (please help)

Set the format of your field to Number with 2 decimal points.

• ###### 8. Re: Custom calculation script for a lease (please help)

Duh!  my head was stuck in trying to do it with js.    Thank you again!!