I'm trying to write a search in a SuiteScript (2.0...
# suitescript
s
I'm trying to write a search in a SuiteScript (2.0) to find things in a list of values. It happens to be an
inventorynumbers
search, where I have a list of inventory numbers, and I'm searching to find their IDs, but the question itself is very general: How can i filter on a list of values? I tried
['inventorynumber', 'anyof', ['SN1', 'SN2']]
but it gives me
UNEXPECTED_ERROR
. Any ideas?
b
wrong type of filter
use the text type filters like
is
in code, i personally like using filter expressions:
Copy code
[
  ["inventorynumber", "is", "SN1"],
  "OR",
  ["inventorynumber", "is", "SN2"],
]
s
i thought
is
could only work on one value?
In my case, using the
OR
syntax might be annoying because the array is a variable length, so I'd have to write a whole function just to build the filters up. What is a text type filter? Am I able to still use an array of strings to filter or is
OR
the only way?
I am trying
is
right now though, maybe it will work
b
a somewhat slower alternative is a formula using the in operator in a formula:
Copy code
[
  "formulanumeric: CASE WHEN {inventorynumber} IN ('SN1', 'SN1') THEN 1 ELSE 0 END",
  "equalto",
  "1",
]
keep in mind you will need to split it up into multiple filters if there are over 1000 values in the
in
s
ah ok i figured there must be a way to do it with a formula. But you recommend the OR syntax over the formula? Either way ill have to modify the array (I did confirm that IS only finds the first value)
b
different types of columns support different types of operators
s
Oh wow thanks! I've never seen this page before. So basically since this is a text value I can only use the operators in the text column, and anyof isnt one of them huh :(( ok I will try one of these solutions though thank you so much!
s
Also there is nothing wrong with having to write a function to dynamically add filters with `OR`'s, it's a good thing to get used to when doing more complicated searches. Also the
OR
method may be faster the larger the dataset gets, formula's in criteria slow the search down.
s
Thanks, I'm going to try the
OR
way 🙂