Netsuite SuiteScript : Iterating Over A Search Result Array

Working on some new tech for a client that is running Oracle Netsuite. One of the tasks is to create a tool for locating the nearest dealer for an online order. This is going to require pulling data from Netsuite to generate reports. Turns out Netsuite has a pretty extensive customization subsystem using a quasi-JavaScript stand in called “SuiteScript”.

I say “quasi-JavaScript” because Oracle appears to have bastardized this interface enough for it to not really be JavaScript but is “mostly” JavaScript. Part of that is likely my lack of depth of knowledge of the sandbox they’ve put SuiteScript in for the “Scriptlets”. Part of it is Oracle’s amazingly extensive set of documentation pages that contain very little actually useful content. It is the technical equivalent of reading a novel that doesn’t tell a story.

As such I’ve found myself having to craft my own “cheat sheet” of notes about how to actually utilize and employ SuiteScript.

For those of you that do not have a Netsuite subscription, sorry — but half these links will not work as you need a login to even read the documentation (come on Oracle, really? What year is this?). For those that do have an account, I’m referring to the current SuiteScript 2.x API here.

Iterating Over A Search Result Array

To craft a search of existing data you use the N/Search SuiteScript module and fetch results as follows…

  • Use the create() method of N/Search to build a search.Search object
  • Use the run() method on the search.Search object to get a search.ResultSet object
  • Use the getRange() method on the search.ResultSet object to get back an array of search.Result objects (the underlying data.

In code it looks something like this — the define/require and wrapper method to import N/Search is not included in this snippet…

    /**
     * Get the online (web) orders
     *
     * @var {search.Search} nsSearch
     * @var {search.ResultSet} searchResults
     * @var {search.Result[]} resultSubset
     *
     * @returns {search.Result[]}
     */
    const fetchOnlineOrders = () => {
            const nsSearch = search.create({
                type: search.Type.CASH_SALE
            });
            const searchResults = nsSearch.run();
            const resultSubset = searchResults.getRange({
                start: 0,
                end: 10
            });

            return resultSubset;
        }

According to the Netsuite documentation, this function should return an array of search.Result[] objects.

So we should be able to easily iterate over this with a basic JavaScript array method. But alas, this is not the case.

In JavaScript you should be able to iterate over an array of objects as follows:

/** @var {search.Result[]} onlineOrders **/
const onlineOrders = fetchOnlineOrders();

...

onlineOrders.forEach( (row) => {
                list.addRow(row);
});

However this does not work in SuiteScript. Apparently Oracle does not implement standard iterators for their search.Result object.

As such you’ll need to iterate over the array in old-school basic JavaScript as follows:

            for (let rowNum; rowNum < onlineOrders.length; rowNum++ ) {
                list.addRow(onlineOrders[rowNum]);
            }

The first less of working in SuiteScript — don’t assume Oracle has implemented a full interface for their native Netsuite objects. Iterators are often missing from objects that are returned as arrays.

image via Pixabay

Leave a Reply

%d bloggers like this: