8 Replies Latest reply on Jan 18, 2018 1:38 PM by alicel64594251

# Need help with if then statement

I have a calculated field "C". It is the result of subtracting two other fields, "A" and "B".

I need the value of "C" to populate a new field, "D" if the value of "C" is > 0 and populate "E" if the value of "C" is < 0.

I'm sure this is simple, but I would appreciate your help.

Thanks!

• ###### 1. Re: Need help with if then statement

You'll need to use a Custom JavaScript calculation for this:

```event.value = this.getField("A").value - this.getField("B").value;
if(event.value > 0)
this.getField("D").value = event.value;
else if(event.value < 0)
this.getField("E").value = event.value;
```

This code does exactly what you requested. However, it does not cover the situationd you did not mention

1. What happens when "C" == 0 ?

2. What happens to D and E when the other field gets the value?

• ###### 2. Re: Need help with if then statement

If the answer is 0, nothing needs to go in either field, since no money will go to anyone.

But your question about what happens to the other field once we know where the answer goes is spot on. Only one field needs to be filled in--"D" if the answer is positive or "E" if the answer is negative. How can I get it to give me only one answer? It seems that since I have to type in the "A" field info first, it automatically assumes the result will be positive, even if the final result is negative (because there are more debits than credits in the fields)--see below

(Lastly, is there a way to get rid of the minus sign ("-") for the Amount due? If not, no worries. You've been a big help!

• ###### 3. Re: Need help with if then statement

Thom's code is good, but not sufficient. It should also clear those fields when the answer needs to go to the over one. Use this version, instead:

```event.value = Number(this.getField("A").value) - Number(this.getField("B").value);
if (event.value > 0) {
this.getField("D").value = event.value;
this.getField("E").value = "";
} else if (event.value < 0) {
this.getField("D").value = "";
this.getField("E").value = Math.abs(event.value);
} else if (event.value==0) {
this.getField("D").value = "";
this.getField("E").value = "";
}
```

Edit: Code fixed

• ###### 4. Re: Need help with if then statement

Thanks. I plugged the new info in and got an error message:

• ###### 5. Re: Need help with if then statement

if (event.value > 0) {

• ###### 6. Re: Need help with if then statement

I must be doing something wrong. I added the opening bracket, but now get this error message:

• ###### 7. Re: Need help with if then statement

You added it in the wrong place. Copy the code from my previous reply. I fixed it there...

• ###### 8. Re: Need help with if then statement

I figured I'd messed it up!

Thanks so much! It works perfectly!