13 Replies Latest reply on Jan 14, 2018 8:30 AM by stepheng54012748

If then question

If anyone can help with a scenario that I'm struggling with I would be eternally grateful.  I am learning how to write in Javascript but am very new to this.  I'll do my best to describe what is happening and how I'm trying to write the script.  Below is the breakdown:

Line "J10" is a sum of 5 lines.

Line "J13" is the sum of line "J10" and "J12" ONLY if Line "J10" is greater than 0.  If Line "J10" is a negative number then Line "J13" should return 0.  I've written the script so many different ways I think I've thoroughly confused myself.  At one point, I thought I solved it.  When I enter the number and it gave the result I was looking for on the positive side and then changed the number to a negative but the result from the previous script didn't change but stayed the same.  I want to be able to write the Javascript so if I test it with a positive result it gives the result I'm looking for and if I change the test number to a negative number that it switches to a 0. It's almost as if the script is unstable.  Any input on how to write this would be so greatly appreciated.  Thank you in advance.

Here is the latest that doesn't work.

var theField = this.getField("V1");

var theValue = theField.value;

var theField2 = this.getField("V2");

var theValue2 = theField.value2;

if (theValue > 0) {

this.getField("J13").value= theValue2;

}

else {

this.getField("J13").value= 0";

}

• 1. Re: If then question

A field has no property with the name 'value2'.

• 2. Re: If then question

Thank you for the response.  I understood that I'm getting that error.  Any suggestions what I can write to eliminate the error and get the result I'm looking for?

• 3. Re: If then question

Change that line to:

var theValue2 = theField2.value;

• 4. Re: If then question

Thank you, the resolved that problem.  Interesting, the script works, however, if I change thevalue  the result remains the same and doesn't update with the new value.  Is there a script or a an alteration I can put in my script that will update when I'm changing the value?  Thank you again.

var theField = this.getField("V1");

var theValue = theField.value;

var theField2 = this.getField("V2");

var theValue2 = theField2.value;

if (+theValue > .001) {

this.getField("J13").value= +theValue2;

}

else {

this.getField("J13").value= "";

}

• 5. Re: If then question

One other thing sometimes when I put a value in nothing is computing for my final result.  If I tweak it suddenly the result appears.  Not sure why it doesn't populate result and then it appears all of a sudden.

• 6. Re: If then question

Check the field calculation order in the form.

• 7. Re: If then question

Great advice everyone!  Everything seems to be in working order.

Thank you!

• 8. Re: If then question

Also, if this is a calculation script for "J13" replace these lines:

this.getField("J13").value = ...

With:

event.value = ...

• 9. Re: If then question

More great advice.  Of course, now I ran into another error.  If there is any advice on this I think this will function exactly the way i'd like.  Once I implemented the changes everything works except I added one more thing.  So, I have an "if" statement that provides me an event result, and I have an else statement that provides me a result, I then added one more "else" statement below.  I tried make this an event.value result but I still get the same syntax error JavaScript error: SyntaxError: expected expression, got keyword 'else' on line 26.  All I'm trying to do is get a result of "o" if the "if" or "else" or not true.

}

else {

this.getField("J13").value="";

}

• 10. Re: If then question

• 11. Re: If then question

Here is what I have so far:

var theField = this.getField("J5");

var theValue = theField.value;

var theField2 = this.getField("J6");

var theValue2 = theField2.value;

var theField3 = this.getField("J7");

var theValue3 = theField3.value;

var theField4 = this.getField("J8");

var theValue4 = theField4.value;

var theField5 = this.getField("J9");

var theValue5 = theField5.value;

var theField6 = this.getField("J11");

var theValue6 = theField6.value;

var theField7 = this.getField("J12");

var theValue7 = theField7.value;

if (+theValue <= 0) {

event.value = (+theValue + +theValue2 + +theValue3 + +theValue4 + +theValue5);

}

else {

event.value > (+theValue + +theValue2 + +theValue3 + +theValue4 + +theValue5 + +theValue6 + +theValue7);

}

else {

this.getField("J13").value="";

}

• 12. Re: If then question

Following line makes no sense:

event.value > (+theValue + +theValue2 + +theValue3 + +theValue4 + +theValue5 + +theValue6 + +theValue7);

You can't use 2 else

You must use something like this:

if (...) {

} else if ( ... ) {

} else {

}

• 13. Re: If then question

Working perfectly now!! Thank you