Does anyone know any reason why a search that star...
# suitescript
e
Does anyone know any reason why a search that starts with this would return any transaction types other than Sales Order? We just found a weird scenario where when using certain filters other transaction types (Invoice, Return Auth) would get returned as results. Our understanding was that this search would only return SALES_ORDER types, so it wasn't required to add filtering by type in the filters list.
Copy code
search.create({
                type: search.Type.SALES_ORDER,
b
a sales order search should be equivalent to a transaction search with a type filter
e
Yep, that's what I thought as well. But, we have discovered an issue trying to write a search that searches for content in the billaddress or shipgaddress fields of a SALES_ORDER search.
Copy code
var orderSearch = search.create({
                type: search.Type.SALES_ORDER,
                columns: [
                    'entity',
                    'status',
                    'trandate',
                    'tranid',
                    'amountremaining',
                    'amountpaid',
                    'total',
                    'internalid'
                ],
                filters: [
                   ['mainline','IS','T'],
                    'and',
                     ['billaddress','CONTAINS','joanna'],
                      'or',
                     ['shipaddress','CONTAINS','joanna']
                ]
            }).run().getRange({start:0, end: 100});
This search will return multiple transaction types in our env, not just sales orders. If we correctly add the brackets around the billaddress and shipaddress filters, then it does not return other transaction types. It as though that hanging OR causes the search to lose the type filter altogether. I'd love to know if anyone else can reproduce this.
b
Save the search so you can see how the filters work
Beware the order of operations, and is above or
e
I dumped the search out to JSON and it looks correct. It looks exactly as I would expect it to. It is not losing the salesorder filter, which was what I assumed might be happening.
Copy code
{
  "type": "salesorder",
  "id": -1,
  "filters": [
    {
      "name": "mainline",
      "operator": "IS",
      "values": [
        "T"
      ],
      "isor": false,
      "isnot": false,
      "leftparens": 0,
      "rightparens": 0
    },
    {
      "name": "billaddress",
      "operator": "CONTAINS",
      "values": [
        "joanna"
      ],
      "isor": true,
      "isnot": false,
      "leftparens": 0,
      "rightparens": 0
    },
    {
      "name": "shipaddress",
      "operator": "CONTAINS",
      "values": [
        "joanna"
      ],
      "isor": false,
      "isnot": false,
      "leftparens": 0,
      "rightparens": 0
    }
  ],
  "columns": [
    {
      "name": "entity",
      "join": null,
      "summary": null,
      "label": null,
      "type": null,
      "function": null,
      "formula": null,
      "sortdir": "NONE",
      "whenorderedby": null,
      "whenorderedbyjoin": null,
      "whenorderedbyalias": null
    },
    {
      "name": "status",
      "join": null,
      "summary": null,
      "label": null,
      "type": null,
      "function": null,
      "formula": null,
      "sortdir": "NONE",
      "whenorderedby": null,
      "whenorderedbyjoin": null,
      "whenorderedbyalias": null
    },
    {
      "name": "trandate",
      "join": null,
      "summary": null,
      "label": null,
      "type": null,
      "function": null,
      "formula": null,
      "sortdir": "NONE",
      "whenorderedby": null,
      "whenorderedbyjoin": null,
      "whenorderedbyalias": null
    },
    {
      "name": "tranid",
      "join": null,
      "summary": null,
      "label": null,
      "type": null,
      "function": null,
      "formula": null,
      "sortdir": "NONE",
      "whenorderedby": null,
      "whenorderedbyjoin": null,
      "whenorderedbyalias": null
    },
    {
      "name": "amountremaining",
      "join": null,
      "summary": null,
      "label": null,
      "type": null,
      "function": null,
      "formula": null,
      "sortdir": "NONE",
      "whenorderedby": null,
      "whenorderedbyjoin": null,
      "whenorderedbyalias": null
    },
    {
      "name": "amountpaid",
      "join": null,
      "summary": null,
      "label": null,
      "type": null,
      "function": null,
      "formula": null,
      "sortdir": "NONE",
      "whenorderedby": null,
      "whenorderedbyjoin": null,
      "whenorderedbyalias": null
    },
    {
      "name": "total",
      "join": null,
      "summary": null,
      "label": null,
      "type": null,
      "function": null,
      "formula": null,
      "sortdir": "NONE",
      "whenorderedby": null,
      "whenorderedbyjoin": null,
      "whenorderedbyalias": null
    },
    {
      "name": "internalid",
      "join": null,
      "summary": null,
      "label": null,
      "type": null,
      "function": null,
      "formula": null,
      "sortdir": "NONE",
      "whenorderedby": null,
      "whenorderedbyjoin": null,
      "whenorderedbyalias": null
    }
  ],
  "settings": null,
  "title": null,
  "scriptId": null,
  "isPublic": false,
  "packageId": null
}
I occasionally use the "Saved Search Export to SuiteScript 2.x" that works quite well. I noticed that they still add the type filter in the filter expression. Makes me wonder if they learned this was necessary. This was how that extension built the same query I created in the UI.
Copy code
const salesorderSearch = search.create({
  type: 'salesorder',
  filters: [
    ['type', 'anyof', 'SalesOrd'],
    'AND',
    ['mainline', 'is', 'T'],
    'AND',
    [
      ['billaddress', 'contains', 'Joanna'],
      'OR',
      ['shipaddress', 'contains', 'Joanna'],
    ],
  ],
b
There is no actual sales order saved search
A sales order saved search is just a transaction search with a type filter
The behavior is more clear in the ui, and the placement of the type filter was probably unfavorable with the order of operations
e
Yep, that definitely makes sense based on what I'm seeing. But, any idea why they would even bother allowing you to specify certain transaction types then? Why not just have a search.Type.TRANSACTION?
Otherwise, it leads developers to believing that they are already filtering down to that type.
b
Most of suitescript matches what you see in the ui
There are searches for each of the transaction types in the ui, so there are searches for each of the transaction types in script
e
Thanks for help @battk
This still didn't make 100% sense. But, we determined that there is definitely a NetSuite bug by taking our SS saved search and saving it so that we could view it in the UI. If you use an OR in your saved search filter without proper bracketing, it can potentially override the type filter that NetSuite adds automatically.
This is how it shows up in the UI. We didn't add that first type filter. NetSuite added that automatically. But, you can see that it's definitely going to return results from other transaction types.
b
as i said, unfavorable order of operations