Netsuite Searching with SuiteScript 2.0
Tags: netsuite, suitescript20, suitescript, javascript
Published: 2019-02-18 20:37:48

---
title: Netsuite Searching with SuiteScript 2.0
published: true
description: So you want to learn about Netsuite SuiteScript 2.0 searching?
tags: netsuite, suitescript 2.0, Suitescript, Javascript
cover_image: https://pcxcom.com/wp-content/uploads/2015/10/seminar_1000.jpg
---

What even is Netsuite?

Netsuite is fast becoming the most popular cloud-based (cloud-based; noun, it's just a flipping web site on the internet... *sigh*) ERP platform.  ERP stands for Enterprise Resource Planning.  So it will handle inventory, human resources, manage an online store.  You get the idea.  The cool thing about Netsuite is that it's EXTREMELY customizable for the user.  You can add fields, scripts, records... Many solution providers make a living out of providing services that plug right into their platform.  

With that out of the way, Netsuite provides a developer with a large toolset to interact with their platform. They call it "SuiteScript" but what it really is, is Javascript but built into their server's infrastructure.  

They have a variety of types you can code against also.  Client-side, scheduled, Mass update, Map/Reduce, User events and a few others... Each type interacts with Netsuite a little bit differently based on what you're aiming to do.

In my example, I want to focus on a user event script.  For brevity, it is just a script you can assign to run either before a page loads, before submit or after submit.  

For starters, Netsuite has pretty good documentation now but I want to offer up something a little more simple.

Using an asynchronous module definition (AMD) philosophy, we can plug in what we need for our custom script.  For more information on this, please read [this](https://requirejs.org/docs/why.html) and it will give more background on it.

How this is done is we define our modules, assign them a variable and use them within the sealed function.

```
/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/search', 'N/log'],
    function(search, log) {
        return {beforeLoad: beforeLoad};

        function beforeLoad(context){

            var srch = search.create({type: 'itemfulfillment', filters: [], columns:[]};

            var srchResults = srch.run();
            
            srchResults.each(function(result){
                log.debug('Result', result.id);
            });
        }
    }
);
```

So let's step through what's going on:

```
/**
 *@NApiVersion 2.x   <--- Required to declare what API version (2.0, 2.x or 2.X)
 *@NScriptType UserEventScript <--- Required to declare what type of suitescript this is, in this case, a UserEventScript
 */
```

```
define(['N/search', 'N/log'], <--- Defining what Netsuite modules we wish to use The 'N/search' and 'N/log' are my most utilized modules
    function(search, log) { <-- Defining the globals to the modules we defined above
```

```
        return {beforeLoad: beforeLoad} <--- Returning the callback function to the assignable return variable. In this case, beforeLoad is being used.  beforeSubmit and afterSubmit is also available to us.

```

We can also change how we declare this by just writing the callback function in line with the assignment by doing the following.  I find it more readable buy referencing the function instead of writing it inline but that's a personal preference.

The context parameter is standard for the UserEventScript and contains valuable information for us such as script information or other entry point information specific to that script.

```
        return {beforeLoad: function(context){/* stuff */}};
```

Now the meat and potatoes.  The callback function that is running for the beforeLoad

```

        function beforeLoad(context){

            // Initialize an array of filter objects based on a (name, condition, value struct)
            var myFilters = [{'internalid', 'is', '1234'}];

            // Initialize an array of columns you want to return
            var myColumns = ['name'];

            // Create search object by declaring the type and any filters and columns that you want to return
            var srch = search.create({type: 'itemfulfillment', filters: myFilters, columns: myColumns};

            // Run the created search (the run routine only runs 1000 max so if you need more, you would want to use the runPaged() routine 
            var srchResults = srch.run();
            
            // Loop through the array of results using either by inlining the function callback or by assigning the function to the each function.  In this scenario, I do use the inline callback for readability. I'm weird and inconsistent that way.
            srchResults.each(function(result){
                // Log to the script's execution log in debug verbosity of the result and it's build in id property
                log.debug('Result', result.id);
            });
        }
```

That's really the most basic usage of a search in Suitescript 2.0.  It's a little more complex than Suitescript 1.0 but it offers a much more modular and flexible design pattern.

I hope this helps any noobs learning Netsuite's SuiteScript. I know me being self-taught, these types of articles were a Godsend to me to help explain code more than a generic API document of functions and properties.