Copy link to clipboard
Copied
I am trying to write a code that looks at a field and if it equals a specific number (in this case "2"), then it divides another field by that number. Here is what I have so far...
if (this.getField("Tex3").value != 2) {
var numerator = +getField("text1").value;
var denominator = +getField("text2").value;}
// Calculate and set this field value
if (denominator !== 0)
{
event.value = numerator / denominator;
}
else {
event.value = "";
}
It is currently returning nothing.
Copy link to clipboard
Copied
Is "Tex3" the correct field name?
Initialize variables outside 'if' statement, or it will throw an error when 'if' condition is not met, and it tries to calculate.
Copy link to clipboard
Copied
My apologies. The field name should be "Text3". I caught that and corrected it right after I posted (of course), but my results were the same.
Can you explain or give an example of what you mean by "Initialize variables outside 'if' statement"? I'm terribly new to all of this.
Copy link to clipboard
Copied
Pay attention to field name because "Text1" and "text1" are not same.
Use this:
var t1 = Number(this.getField("text1").valueAsString);
var t2 = Number(this.getField("text2").valueAsString);
var t3 = Number(this.getField("Text3").valueAsString);
if(t3 === 2){
if(t2 !== 0)
event.value = t1/t2;
else
event.value = "";}
else
event.value = "";
Copy link to clipboard
Copied
Also replace "!=" with "==".
Copy link to clipboard
Copied
In addition to the correct comments made above, you need to define what should happen if the value of the first field is not 2. If you don't do anything then the current value will remain as it was, which is probably not what you want to happen, unless you want the user to be able to fill in a value manually in that case.
Copy link to clipboard
Copied
I would like it to return the field as blank. How do I accomplish this?
Copy link to clipboard
Copied
At the end of the code add this:
else event.value = "";
Copy link to clipboard
Copied
UPDATED CODE:
if (this.getField("Text3").value == 2)
else {
var numerator = +getField("undefined_10").value;
var denominator = +getField("Text3").value;}
// Calculate and set this field value
if (denominator == 0)
{
event.value = numerator / denominator;
}
else {
event.value = "";
}
}
Now I am getting a syntax error 2: at line 3
Copy link to clipboard
Copied
This still has a lot of the issues we described... Try this:
if (Number(this.getField("Text3").value) == 2) {
var numerator = Number(this.getField("undefined_10").value);
var denominator = Number(this.getField("Text3").value);
if (denominator == 0)
event.value = "";
else event.value = numerator / denominator;
} else {
event.value = "";
}
Copy link to clipboard
Copied
Thank you. I have tried this, but I am still not getting the desired results.
For further information, the "Text3" field is populated by default as blank or, if a check box is checked, populated with the number 2. The check box portion is working fine, but when the box is checked and I am getting a result of blank, but when the check box is UNchecked, I am getting the result that I actually need. I don't know if the check box is causing an issue. I've attached a screenshot of the project that will hopefully help.
I appreciate your help.
Copy link to clipboard
Copied
Does you use a script at the checkbox?
Copy link to clipboard
Copied
Script in Text3
if (this.getField("CheckBox2").value != "Off") {
// box is checked
event.value = 2;
}
else {
// box is unchecked
event.value = "";
}
Copy link to clipboard
Copied
Change the field calculation order.
Copy link to clipboard
Copied
Thank you. I've tried, but I can't seem to get it right. I hate to be so needy, but could you please post how the script should look?
Copy link to clipboard
Copied
The script is ok. Change the field calculation order.