SuiteScript Filter Expression Example

I had trouble getting the ‘anyof’ search operator to work in a complex SuiteScript 2.0 filter expression. Perhaps it’s possible. However, I got tired of messing with it. Here is my workaround.


I also needed my results to be unique. I supplied a list of item names and only had room to associate one internal ID with each name in that list. Here is how I solved that problem.


In my initial test, I included 190 item names in my arrayOfNames. All names in that list matched items where display names did not match item name in the databsae. Some had ‘(COPIED)’ in the item name and were still active. In summary, it was a good sample set.

My test ran is a couple of seconds. It matched 100% of my arrayOfNames (190 out of 190). We have about 40,000 items in our item’s table in NetSuite. This was in sandbox too! So, it was pretty fast.

Here is the code in a form you can paste directly into debugger.

require([‘N/log’, ‘N/search’],
    function (log, search) {
        function getItemIdsByName(arrayOfNames) {
            var myFilters = new Array();
                function (name) {
                    if (myFilters.length > 0) myFilters.push(‘or’);
                        [‘itemid’, ‘is’, name],
                        [‘displayname’, ‘is’, name]
            var mySearch = search.create({
                type: search.Type.ITEM,
                columns: [‘internalid’, ‘itemid’, ‘displayname’],
                filters: [
                    [‘isinactive’, ‘is’, false],
                    [‘itemid’, ‘doesnotcontain’, ‘(COPIED)’],
            var myPages = mySearch.runPaged({ pageSize: 1000 });
            var matches = new Array();
            for (var i = 0; i < myPages.pageRanges.length; i++) {
                var myPage = myPages.fetch({ index: i });
                    function (match) {
                        var itemid = match.getValue(‘itemid’);
                        var displayname = match.getValue(‘displayname’);
                        var internalid = match.getValue(‘internalid’);
                            itemid: itemid,
                            displayname: !displayname ? itemid : displayname,
                            internalid: internalid
                        return true;
            var results = new Array();
                function (name) {
                    var firstMatch = matches.filter(
                        function (a) {
                            // If the itemid or the displayname matches a name in arrayOfNames, it matches.
                            if (a.itemid === name) return true;
                            if (a.displayname === name) return true;
                            return false;
                    if (firstMatch && firstMatch.length > 0) {
                            itemid: name,
                            internalid: firstMatch[0].internalid
            return results;
        var arrayOfNames = [
            ‘Your Item Name here #1’,
            ‘Your Item Name here #2’,
            ‘Your Item Name here #3’,
            ‘Your Item Name here #190’,
        var matches = getItemIdsByName(arrayOfNames);
        // The log function is slow. Don’t call it more than once if you are
        // interested in response time.
        log.debug(‘Matched ‘ + matches.length + ‘ out of ‘ + arrayOfNames.length);
        // If you want to see results and not measure response time.
        // matches.forEach(
        //     function (match) {
        //         log.debug(‘itemid: ‘ + match.itemid + ‘, internalid: ‘ + match.internalid);
        //     }
        // )

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s