Using the ‘N/search’ SuiteScript Module

NetSuite for Developers

Searches in SuiteScript 2.0

The ‘N/search’ SuiteScript Module is an important key to coding effectively in NetSuite. Although there are numerous methods that this module supports (which the help documentation defines in detail), we will just address the three most commonly used methods here in this SuiteScript tutorial. The first two methods we will learn are closely related to the NetSuite “Saved Search” user interface, while the last one is an exclusive SuiteScript specialty.

Creating Searches with “search.create()”

As always, be sure to define your module at the beginning of your server-side or client script. The structure of your script should look something like the following, here using a User Event script as an example:

define(['N/search'], function (search) {
    /**
     * Example script to demonstrate 'N/search' module.
     * 
     * Deploy To: Sales Orders
     * @NApiVersion 2.x
     * @NScriptType UserEventScript
     * @author Ben Rogol (SuiteRep)
     */
    var exports = {};

    function beforeSubmit(context) {
        // Do stuff here!
    }

    exports.beforeSubmit = beforeSubmit;
    return exports;
});

After having a general template and defining our modules, we can begin testing out our search methods. The method search.create() creates a search in a way very similar to the native NetSuite Saved Search user interface.

First, we define our search along with the filters (aka. criteria) and columns (aka. results):

// search.create()
var mySearch = search.create({
    type: 'salesorder',
    filters: [
        ["type","anyof","SalesOrd"], 
        "AND", 
        ["mainline","is","T"]
    ],
    columns: [
        search.createColumn({name: "entity", label: "Name"}),
        search.createColumn({name: "trandate", label: "Date"})
    ],
});

Here we made a search for Sales Orders with columns/results that show the entity name and Sales Order date. Next, we need to run our search so we can do things with the results. There are two common ways to run this search.

 

Running Search: Technique #1

var results = mySearch.run().getRange({
    start: 0,
    end: 50
});

for (var i = 0; i < results.length; i++) {
    log.debug('Hello World #' + i);
}

In this method, we get a range for our search results. We start at 0 (the first result) and end at 50 results. This will keep the script from being overloaded if there are too many results. After this, we loop over our results and log “Hello World” for each.

 

Running Search: Technique #2

var searchResultCount = mySearch.runPaged().count;
log.debug("mySearch result count", searchResultCount);

var results = mySearch.run().each(function(result){
    log.debug('Hello World');
})

A Helpful Community Tool

Developer David Smith has created an extremely helpful Chrome extension tool that can be used to export searches from the NetSuite user interface. I really recommend that you try this incredible tool out, especially as you learn the ropes of the NetSuite ‘N/search’ SuiteScript Module.

Loading Searches with “search.load()”

This method is similar to “search.create(),” except the search is already created in the NetSuite UI. All we have to do is load the search and run it in our script.

var mySearch = search.load({
    id: 'customsearch_my_so_search'
});

var results = mySearch.run().getRange({
    start: 0,
    end: 50
});

for (var i = 0; i < results.length; i++) {
    // Return "Hello World" for each result :)
    log.debug('Hello World #' + i);
}

The only thing needed is the internal ID of the already existing NetSuite Saved Search. After this, you can run the search in either way explained above.

Looking up Fields with “search.lookupFields()”

Sometimes we need certain fields on a record, but creating or loading an entire Saved Search would be too heavy for our script. There is a very helpful method called “search.lookupFields()” that really comes in handy as we’re scripting. Here’s what it looks like to search for fields on a Sales Order:

var salesOrderId = 22;

var myFieldsSearch = search.lookupFields({ 
    type: 'salesorder', 
    id: salesOrderId, 
    columns: ['entity', 'trandate'] 
});

var entityName = myFieldsSearch.entity;
var tranDate = myFieldsSearch.trandate;

log.debug("entityName", entityName);
log.debug("tranDate", tranDate);

For the sake of example, we hard-coded the internal ID of the Sales Order. But in your script you would likely have a more dynamic definition. Search.lookupFields takes three parameters: the record type (in this case a Sales Order), the internal ID of the record, and the fields you are searching for. This method returns an object with the results, and we can find those results by the simple myFieldsSearch.entity or myFieldsSearch[“entity”].

Conclusion

We hope you find this NetSuite SuiteScript tutorial helpful–understanding the ‘N/search’ SuiteScript Module opens up many opportunities. As always, be sure to subscribe to our email list to receive helpful NetSuite tips and tutorials every week!