Easy NetSuite Search

In an attempt to expand my knowledge to other platforms than Notes and Domino, I have now been working with NetSuite for a number of months. I have mainly been working with the ERP part of the cloud based system.

The language used is called SuiteScript, and it is Javascript with a NetSuite-specific API to work directly with the databases. Knowing Javascript makes it easy to get started, just like knowing Visual Basic makes it easy to learn Lotusscript. And just like with Lotusscript, you have to learn the NetSuite specific functions.

Since I like my code clean and easy to read (which will make future maintenance easier), I have created a number of functions to encapsulate NetSuite functionality.

The first one I created was to search the database. The search in NetSuite is done by defining the columns (i.e. fields) to return as an array of search column objects. Then an array of search filters is created, and finally the search function is called, specifying what record type to search and passing the two arrays to it as well. This is a lot of code, and with several searching in a script it can be very repetetive, not to mention hard to read.

Here is an example of a traditional NetSuite search:

var filters = [];
filters.push(new nlobjSearchFilter('item', null, 'anyof', item));
filters.push(new nlobjSearchFilter('location', null, 'noneof', '@NONE@'));
var columns = [];
columns.push(new nlobjSearchColumn('internalid'));
columns.push(new nlobjSearchColumn('trandate').setSort());
columns.push(new nlobjSearchColumn('location'));
var search = nlapiSearchRecord('workorder', '', filters, columns);

Using my function, the code wold be simplified to this:

var search = new Search('workorder');
search.addFilter('item', null, 'anyof', item);
search.addFilter('location', null, 'noneof', '@NONE@');
search.addColumn('trandate',true);  // Sort on this column
var search = search.getResults();

The function also support saved searches. Simply add the following line:


There is a limitation in SuiteScript so that a maximum of 1000 records can be returned by a normal search. There is a trick to bypass this, but it requires some extra coding. So I thought why not add this into the function as default? So I did.

Below is the code for the search function. I usually put it in a separate file and reference it as a library in the scripts where I want to use it. This first version does not support more advanced functionality like formulas in the filters. But for most searches this function will be usable.

 * Module Description
 * Version    Date            Author           Remarks
 * 1.00       11 Nov 2016     kmartinsson
 * 1.05       27 May 2017     kmartinsson      Added support for record type in constructor
//***** Encapsulate search functionality *****
function Search(recordtype) {
   this.columns = [];
   this.filters = [];
   // If record type/ID is passed, no need to set it later
   if (recordtype == null || recordtype == "") {
      this.recordType = null;
   } else {
      this.recordType = recordtype;
   // Set internal id of saved search to null
   this.internalId = null;
   // *** Set array of column names to return
   this.setColumns = function(columnArray) {
      for (var i = 0; i < columnArray.length; i++) { // Check if we have an array, used for joins and sorts if (columnArray[i].isArray()) { // We have an array. Now we need to figure out what it contains if (columnArray[i].length > 2) {
               // We have 3 values, must be id, join and sort
               this.addColumnJoined(columnArray[i][0], columnArray[i][1], columnArray[i][2]);
            } else {
               // We have 2 values, can be id + join or id + sort. Let's find out!
               if (typeof(columnArray[i][1]) == "boolean") {
                  // Boolean value in second parameter means sorting
                  this.addColumn(columnArray[i][0], columnArray[i][1]);
               } else {
                  // Not boolean means a join
                  this.addColumnJoined(columnArray[i][0], columnArray[i][1]);
         } else {
   } // end function setColumns

   // *** Add column to existing array of column names
   this.addColumn = function(columnName, sorted) {
      if (sorted == undefined || sorted == null) {
         this.columns.push(new nlobjSearchColumn(columnName));
      } else {
         if (sorted) {
            this.columns.push(new nlobjSearchColumn(columnName)).setSort(true);
         } else {
            this.columns.push(new nlobjSearchColumn(columnName));
   } // end function addColumn

   // *** Add joined column with to existing array of column names
   this.addColumnJoined = function(columnName, joinName, sorted) {
      if (sorted == undefined || sorted == null) {
         this.columns.push(new nlobjSearchColumn(columnName, joinName));
      } else {
         if (sorted) {
            this.columns.push(new nlobjSearchColumn(columnName, joinName)).setSort(true);
         } else {
            this.columns.push(new nlobjSearchColumn(columnName, joinName));
   } // end function addColumnJoined

   // *** Add a filter for the search results
   this.addFilter = function(fieldId, fieldJoinId, operator, value) {
      this.filters.push(new nlobjSearchFilter(fieldId, fieldJoinId, operator, value));
   } // end function addFilter

   // *** Set the type of record to search for (default is null)
   this.setRecordType = function(recordType) {
      this.recordType = recordType;
   } // end function setRecordType

   // *** Set the saved search to use (internal id, default is null)
   this.useSavedSearch = function(internalId) {
      this.internalId = internalId;
   } // end function useSavedSearch

   // *** Return search results, supports >1000 results through nlapiCreateSearch
   this.getResults = function() {
      var results = [];
      if (this.internalId != null) {
         // If internal id of a saved search is provided, load 
         // that saved search and create a new search based on it
         var savedsearch = nlapiLoadSearch(this.recordType, this.internalId);
         // Add new filters to saved filters
         var newfilters = savedsearch.getFilters().concat(this.filters);
         // Add new columns to saved columns
         var newcolumns = savedsearch.getColumns().concat(this.columns);
         // Perform the search
         var newsearch = nlapiCreateSearch(savedsearch.getSearchType(), newfilters, newcolumns);
      } else {
         // Otherwise build the search ad-hoc and set columns and filters
         var newsearch = nlapiCreateSearch(this.recordType, this.filters, this.columns);
      var resultset = newsearch.runSearch();
      // Loop through the search result set and build a result array
      // so the search can return more than 1000 records.
      var searchid = 0;
      do {
         var resultslice = resultset.getResults(searchid, searchid + 800);
         for (var rs in resultslice) {
      } while (resultslice.length >= 800);
      return results;

   } // end function getResults

} // end class search


This Post Has One Comment

Leave a Reply