0

I have the following SuiteScript 2.0 code in a Suitelet where I would like to add an additional filter to the loaded saved search (inventory items sublist and main record of the Inventory Adjustment record):

  var rs = s.load({
    id: "customsearch_inv_adj_item_search"
  });

  // Copy the filters from rs into defaultFilters.
  var defaultFilters = rs.filters;
  var customFilters = [
     s.createFilter({
        name: "internalid",
        operator: s.Operator.IS,
        values: request.parameters.custscript_report_context.toString()
     }),
  ];

  // Push the customFilters into defaultFilters.
  defaultFilters.push(customFilters);

  // Copy the modified defaultFilters back into rs
  rs.filters = defaultFilters;

  var results = rs.run().getRange(0, 1000);

However the code keeps on failing on the line rs.filters = defaultFilters; with the error:

{"type":"error.SuiteScriptError","name":"WRONG_PARAMETER_TYPE","message":"Wrong parameter type: filters[2] is expected as Filter. ","stack":["createError(N/error)","onRequest(/SuiteScripts/sui_custom_pdf_report.js:308)","createError(N/error)"],"cause":{"name":"WRONG_PARAMETER_TYPE","message":"Wrong parameter type: filters[2] is expected as Filter. "},"id":"","notifyOff":false,"userFacing":true}

Instead of request.parameters.custscript_report_context.toString() I have tried 981, "981", ["981"] but no luck.

custscript_report_context is of type integer and it works fine in trying to load the record via N\record.

I am returning internalid in my saved search as a column.

Does anyone know what I'm doing wrong?

Superdooperhero
  • 7,584
  • 19
  • 83
  • 138

2 Answers2

4

You cannot push an array in searchFilters, only filter objects. Arrays are required if you use filter-expressions.

Try the following code

var rs = s.load({
  id: "customsearch_inv_adj_item_search"
});

// Copy the filters from rs into defaultFilters.
var defaultFilters = rs.filters;

// Push the customFilters into defaultFilters.

defaultFilters.push(s.createFilter({
  name: "internalid",
  operator: s.Operator.IS,
  values: request.parameters.custscript_report_context.toString()
}));
// Copy the modified defaultFilters back into rs
rs.filters = defaultFilters;

var results = rs.run().getRange(0, 1000);
Avi
  • 2,014
  • 1
  • 9
  • 21
0

Similar to other answers...while you can't push an array as mentioned in @Avi 's answer, you can concatenate the arrays as such, eliminating the need to loop through them. This is kind of drawn out, just to show the concept:

 var additionalFilters = new Array();;
                 additionalFilters.push(search.createFilter({name: 'account', operator: search.Operator.ANYOF, values: mainArgs.
                 additionalFilters.push(search.createFilter({name: 'postingperiod', operator: search.Operator.IS, values:[mainArgs.param_posting_period]}));

                 var mySearch = search.load({id: searchID});
                 if(additionalFilters){

                     //Copy the filters from mySearch into defaultFilters
                     var defaultFilters = mySearch.filters;

                     //We will add the new filter in customFilters
                     var customFilters = [];
                     customFilters = additionalFilters;

                     //We will concatenate the customFilters and defaultFilters, thereby creating a new array called allFilters
                     var allFilters = defaultFilters.concat(customFilters);

                     //We will assign the allFilters array back into mySearch
                     mySearch.filters = allFilters;

                 }

Note: This is an edited version of what I gleaned from a different post / answer

dah97765
  • 679
  • 1
  • 13
  • 29