Netsuite - Saved Search Formula(text) Case statement multiply 2 fields - formula

I'm trying to get the product of 2 fields in the 'ELSE' portion of a CASE statement of a Formula(Text) row in a Netsuite saved search, but I keep getting 'ERROR: Invalid Expression' when I run the search. Current formula is:
CASE WHEN {binonhandcount} = 0 THEN '0' ELSE NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0) END
I've tried to simplify it by doing something like this:
CASE WHEN {binonhandcount} = 0 THEN '0' ELSE 1 + 1 END
But it still fails with an invalid expression error. All of the Googling I've done leads me to believe that this should work, but I can't seem to find my mistake. I'm hoping the extra eyes here can give me a kick in the right direction. Thank you.

The data type returned from the formula needs to match the Formula type selected. You can correct your formula by setting it to a Formula (Numeric) type and simply removing the quotes around the '0' after the first THEN:
CASE WHEN {binonhandcount} = 0 THEN 0 ELSE NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0) END
Or if you really want a text formula for some reason, you can wrap the ELSE statement in a TO_CHAR function:
CASE WHEN {binonhandcount} = 0 THEN '0' ELSE TO_CHAR(NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0)) END

In your NetSuite saved search replace Formula(text) to Formula(Numeric).
And your formula would be:
CASE WHEN {binonhandcount} = 0 THEN 0 ELSE NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0) END
Please remove the string from THEN '0'. You should be fine.

Related

R curly braces syntax [duplicate]

This question already has an answer here:
if {...} else {...} : Does the line break between "}" and "else" really matters?
(1 answer)
Closed 1 year ago.
It works, but I wonder why tis is correct
if (one_time_cost_year_zero != 0) { EIW_TPI_flag = 1
} else {EIW_TPI_flag = 0}
while this results in an error
if (one_time_cost_year_zero != 0) { EIW_TPI_flag = 1 }
else {EIW_TPI_flag = 0}
What's the logic behind that?
Because R would not know that your if -else statement is not finished yet (since only the if () line is also valid R...) compare
1 + 2
+ 3
vs
1 + 2 +
3
In R if we want to split a command over multiple lines we need to either leave a bracket open (as in the if -else example) or have a "hanging" operator at the end of the line (there are also "multiline" strings, but they are not really commands per se)....
Then, the error you are seeing results from the fact that we can't beginn a command with else (like we can't start a command with in, | etc.)
Accordingly, we could also write:
if (one_time_cost_year_zero != 0) { EIW_TPI_flag = 1 } else
{EIW_TPI_flag = 0}

Netsuite CASE WHEN expression in custom search is returning “invalid expression”

Can someone help me with what is wrong with this expression
case When ({item.locationquantitybackordered} > 0 AND {quantitycommitted} > 0)
Then ({item.locationquantitybackordered}+{quantitycommitted})
Else
when {item.locationquantitybackordered} > 0
Then {quantitycommitted} = 0
Else {item.locationquantitybackordered} = 0
{item.locationquantitybackordered}+{quantitycommitted}
End
End
It looks like there are a few things I would fix on this.
First, you don't need ELSE before WHEN. There should only be one WHEN, at the end of the CASE statement as the last condition.
Second, you have two END statements, which isn't necessary - only one is needed per CASE statement. You might need one if you had a nested CASE statement, but you don't so it is unnecessary.
Finally, your ELSE actually doesn't make much sense - you have two statements in there, {item.locationquantitybackordered} = 0 (which is a test, not a value) and {item.locationquantitybackordered} + {quantitycommitted}, which is a proper value, and it is the same as your first case above.
I'd rewrite your statement as:
CASE
WHEN ({item.locationquantitybackordered} > 0 AND {quantitycommitted} > 0) THEN ({item.locationquantitybackordered} + {quantitycommitted})
WHEN {item.locationquantitybackordered} > 0 THEN 0
ELSE {item.locationquantitybackordered} + {quantitycommitted}
END

iferror equivalent in R

Here is the simplified version:
Suppose I am using 'which' function to find a position, say -
position_num=which(df$word=="ABC")
If the value exists it is fine and it returns an integer, but in case it is unable to match it returns integer(0) in such a case I want to assign a default value to position_num=1.
Thanks for the help in advance
Something like the following if() statement would probably do it.
position_num = if(!length(w <- which(df$word == "ABC"))) 1 else w
Here we are just checking to see if the result from which() has a length, because
length(integer(0))
# [1] 0
And we return 1 if it doesn't have a length, and the which() result (w) otherwise.
Would this work for you?
position_num=ifelse(length(which(df$word=="ABC")) != 0,which(df$word=="ABC"),1)

Can't understand the error message in the if condition in R

My program written in R sometimes (not always, but almost always when the simulation is run for a large number of times) shows this error message:
Error in if (sum.wt1y1 == 0 | sum.wt2y2 == 0) zn[k] <- 0 else zn[k] <- (sum.wt1y1 * :
missing value where TRUE/FALSE needed
Can anyone explain me what is the meaning of this error message? I cannot find where the error is. The final output is a vector. Now in that vector up to some values it shows "values" but the rest are 0, 0, 0,..., 0 when the error message appears. If the error message do not appear, then all the positions of the vector is filled up with values (no zeros).
The error arises due to NA values usually:
if (NA == 0) print('foo')
# Error in if (NA == 0) print("foo") :
# missing value where TRUE/FALSE needed
The solution is to remove missing values or include a check for them:
if (!is.na(x) & x == 0) ...
Try this:
which(is.na( sum.wt1y1 == 0 | sum.wt2y2 == 0))
To expand on what #justin said, the error message is essentially telling you that it was expecting a T/F but did not get it.
When you see such an error, the best thing to do is to explore the value that is inside
the parens in your if( .... ) statement.
In this specific case, looking at sum.wt1y1 == 0 | sum.wt2y2 == 0 would probably help you find the culprit.

If else statement comparing to 0 or NA

I have an element in a data frame tmp that may contain either a number, 0, or NA. If that element is neither 0 or NA, I would like something to happen. Otherwise, nothing happens. I imagine it'd look like this:
if ( tmp[2, 19] != (0 || NA) ){
do something
}
I get this error: Error in if (tmp[2, 19] == (0 || NA)) { : missing value where TRUE/FALSE needed. I don't know if it's not possible in R to compare something to both an int and a string or if I'm just using the OR operator wrong. I've tried different variations in different cases but haven't been able to determine the problem. Please help!
As #GSee said in a comment, you need to use is.na:
if(tmp[2, 19] != 0 || is.na(tmp[2, 19])) {
# do stuff
}
You could have discovered this yourself by reading ?"if" and ?NA.

Resources