Search code examples
javascriptif-statementlivecycle-designer

If Else Statements in Javascript for LiveCycle


I am creating a form on Adobe LiveCycle that adds the numbers in different fields. I need to have the final field (Eligible Assets) add all the previous fields but exclude the sum of three of them and one in specific but only if it is greater than 60000. I've written the script as follows for the first part (to sum all the fields) this is in a field I've titled TotalAssets:

this.rawValue =Cash.rawValue+SavingsAccount.rawValue+ChildrensSavings.rawValue+CheckingAccount.rawValue+ValueHome1.rawValue+ValueHome2.rawValue+ValueVehicle1.rawValue+ValueVehicle2.rawValue+ValueVehicle3.rawValue+BusinessAccount.rawValue+BusinessAssets.rawValue+StocksBonds.rawValue+Retirement.rawValue+CDs.rawValue+OtherInvestments.rawValue+OtherAssets.rawValue;

This has worked fine, but the Retirement value if it is greater than 60000 should not be added into the calculation. This is what I've written (EligibleAssets):

if (Retirement.rawValue > 60000) {
Retirement.rawValue = 0; 
} else {
Retirement.rawValue == Retirement.rawValue ; 
}

this.rawValue = TotalAssets.rawValue - (ValueHome1.rawValue+ValueVehicle1.rawValue +Retirement.rawValue);

When I save the form as a PDF the first total of the fields calculates correctly but the second field comes up blank.

If you can spot what I'm missing or doing wrong I would really appreciate any feedback. Thank you!


Solution

  • There are two simple problems that I see here.

    First problem is that you are using == when you should be using =.

    == - check if the left side is equal to the right side. Example: if(x == 5) {

    = - set the left side to the value of the right side. Example: x = 5

    In the first example we leave x alone, but in the second example we change x to 5.

    So your code should look like:

    } else {
        Retirement.rawValue = Retirement.rawValue;
    }
    

    However, when you think about this, this code doesn't actually do anything. Retirement.rawValue will not change.

    This leads us to the second mistake in the code, at least, it looks to me like a mistake.

    if(Retirement.rawValue > 60000) {
        Retirement.rawValue = 0;
    }
    

    This actually changes Retirement.rawValue, which might potentially change what's inside the Retirement field of your form. Worse, its possible that the form would look the same, but act differently when some other field calculates, since you've changed its rawValue. That would be a very tough bug to catch.

    The solution is to create a new variable: http://www.w3schools.com/js/js_variables.asp

    So now we can create a new variable, set that variable to either the retirement amount or nothing, and then add that variable to the other rawValues at the end:

    var retirementOrZero;
    
    if(Retirement.rawValue > 60000) {
        retirementOrZero = 0;
    } else {
        retirementOrZero = Retirement.rawValue;
    }
    
    this.rawValue = TotalAssets.rawValue - (ValueHome1.rawValue + ValueVehicle1.rawValue + retirementOrZero);
    

    Now we have a number that we can name anything we want, and we can change it however we want, without affecting any code but our own. So we start by checking if our retirement value is greater than 60000. If it is greater, we set our variable to 0. Otherwise, we set our variable to the retirement value. Then we add that variable we made, to the home and value cost.

    As a final question, is it supposed to do

    if(Retirement.rawValue > 60000) {
        retirementValueOrZero = 0;
    }
    

    or is it supposed to do

    if(Retirement.rawValue > 60000) {
        retirementValueOrZero = 60000;
    }
    

    Of course, if you are setting it to 60000 instead of setting it to zero, you probably want to name your variable cappedRetirementValue or something like that -- just make sure you rename it everywhere its used!

    Hopefully that helps!

    Edit: You said you're only adding retirement value if it is greater than 60k, so what you want is this:

    if(RetirementValue.rawValue > 60000) {
        retirementValueOrZero = RetirementValue.rawValue;
    } else {
        retirementValueOrZero = 0;
    }