Saved search in the UI returns only exact matches ...
# suitescript
k
Saved search in the UI returns only exact matches of combinations of multiple select options. Using the same criteria in SuiteScript returns all records whether they match or not. Same happens if I LOAD the saved search which works correctly in the UI. How do you correctly match multiple selects in SuiteScript?
Copy code
filters:
   [
      ["custrecord_item_combo","is","6245","6252","6375"]
   ]
m
l
also add all values into an Array
ā˜ļø 1
m
I never use filters with the array syntax, but I think that syntax for multiple values works even though I find it hard to read. I would recommend using the search.createFilter syntax and proper enums to make it easier to read.
e
Seconding Marvin -
is
is only for Checkboxes and Text fields. For (Multi)Select fields, you use
anyof
or
noneof
. Reference: https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/article_4094344956.html I also prefer to use the
Operator
enumeration, but I vastly prefer the Array syntax for Filters.
And Luiz is correct;
anyof
expects multiple values to be in an Array
Copy code
['custrecord_x', search.Operator.ANYOF, [6245, 6252, 6375]]
k
That doesn't explain why it works in the UI but not in SuiteScript. The problem with anyof is if I have combination [a, b, c] and there are records with [a, b, c] and [a, b, c, d] I will get both results even though I only want one of them
actually it would also match [b, g] and [c, f] for that matter
when what I want is only ones that match the exact combination
I think what I'm going to need to do is get the entire list of combinations that have records, then filter by ones that are exact matches using javascript to return a subset of the list.
e
You can also try the
allof
operator instead.
Multiselect are troublesome to work with in filters
thisred 1
k
wouldn't that still catch [a,b,c,d]?
e
Potentially
k
I'll try it
e
Like you said, depending on the typical size of the result set, it might be easier to rough it in with the search filters then refine it with a JS filter afterward
Quick test in my account shows that
allof [a,b,c]
will still catch
[a,b,c,d]
k
Still better than all possible combinations!
e
and FWIW,
is
behaves the same
It's not documented to work for multiselect, but it does 🤷
k
But only in the UI
search.load returns all records as if the filter isn't there at all
e
To do an exact match, you have to make a conjunction (AND) of two filters: 1. An
allof
containing the values you do want to match 2. A
noneof
containing the values you do not want to match Not really feasible if your value space is large - as I imagine is your case here - but it works for something like a small custom list.
In this example, I have one record that has
2, 3, 4
selected, and one record that has
3, 4
selected.
(the
125
and
305
that get logged are the record internal IDs)
m
@Kristopher Wood sorry I misunderstood what you were trying to accomplish in your original question. Your original filter should work as expected when you want to match - when those and only those values are selected. I tested a similar query and it returned the only record I chose those values on as expected. Although I think the more readable syntax is in the second screenshot.
Oh I did a little more testing and confirmed that it does in fact operate like you stated in your first post which is that "is" is operating like "anyof". I would report it as a bug because when you output the filter expression from the UI search it uses the "is" operator. I would use the suggested workaround that @erictgrubaugh suggested.
k
I ended up using @erictgrubaugh’s suggestion and it worked perfectly. šŸ™‚
The only additional step was to array.filter() the result set to only include the exact match. It's a little additional overhead on the vanilla JS side but doesn't use governance units so it's all good.
m
BTW if you swapped to using N/query you could have solved as well.
k
Yeah that looks like a much better solution. I didn't have time to figure it out last time. I'll start out that way next time. šŸ™‚
m
There are certain cases where what you want isn't in N/query but that is really rare now so I use it by default anymore.