N/search Module Script Samples

The following script samples demonstrate how to use the features of the N/search module:

Important:

These samples are designed to run in a NetSuite OneWorld account, so the OneWorld feature must be enabled in your NetSuite account for the samples to work.

Search for Customer Records and Log First 50 Results

The following sample creates a search for customer records. The sample specifies several result columns and one filter, and it logs the first 50 search results.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/search'], function(search) {
    var mySearch = search.create({
        type: search.Type.CUSTOMER,
        columns: ['entityid', 'firstname', 'lastname', 'salesrep'],
        filters: ['entityid', 'contains', 'Adam']
    });

    var myResultSet = mySearch.run();

    var resultRange = myResultSet.getRange({
        start: 0,
        end: 50
    });

    for (var i = 0; i < resultRange.length; i++) {
        log.debug(resultRange[i]);
    }
}); 

          

Search for Sales Order Records

The following sample creates a search for sales order records and saves it. The sample specifies several result columns and two filters.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/search'], function(search) {
    function createSearch() {
        var mySalesOrderSearch = search.create({
            type: search.Type.SALES_ORDER,
            title: 'My SalesOrder Search',
            id: 'customsearch_my_so_search',
            columns: ['entity', 'subsidiary', 'name', 'currency'],
            filters: [
                ['mainline', 'is', 'T'],
                'and', ['subsidiary.name', 'contains', 'CAD']
            ]
        });

        mySalesOrderSearch.save();
    }

    createSearch();
}); 

          

Load a Search for Sales Order Records and Use a Callback Function to Process Results

The following sample loads and runs a saved search for sales order records. The sample uses the each callback function to process the results.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/search'], function(search) {
    function loadAndRunSearch() {
        var mySearch = search.load({
            id: 'customsearch_my_so_search'
        });

        mySearch.run().each(function(result) {
            var entity = result.getValue({
                name: 'entity'
            });
            var subsidiary = result.getValue({
                name: 'subsidiary'
            });

            return true;
        });
    }

    loadAndRunSearch();
}); 

          

Load a Search for Sales Order Records and Return the First 100 Search Results

The following sample loads and runs a saved search for sales order records. The sample obtains the first 100 rows of search results.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/search'], function(search) {
    function runSearchAndFetchResult() {
        var mySearch = search.load({
            id: 'customsearch_my_so_search'
        });

        var searchResult = mySearch.run().getRange({
            start: 0,
            end: 100
        });
        for (var i = 0; i < searchResult.length; i++) {
            var entity = searchResult[i].getValue({
                name: 'entity'
            });
            var subsidiary = searchResult[i].getValue({
                name: 'subsidiary'
            });
        }
    }

    runSearchAndFetchResult();
}); 

          

Load and Run a Paginated Search and Process the Results

The following sample loads and runs a saved search for sales order records. The sample uses the forEach callback function to process the paginated results.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/search'], function(search) {
    function loadAndRunSearch() {
        var mySearch = search.load({
            id: 'customsearch_my_so_search'
        });

        var myPagedData = mySearch.runPaged();
        myPagedData.pageRanges.forEach(function(pageRange){
            var myPage = myPagedData.fetch({index: pageRange.index});
            myPage.data.forEach(function(result){
                var entity = result.getValue({
                    name: 'entity'
                });
                var subsidiary = result.getValue({
                    name: 'subsidiary'
                });
            });
        });
    }

    loadAndRunSearch();
}); 

          

Create a Search for a Custom Record Type

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

The following sample creates a search for a custom record type. To search for a custom record type, you must specify a type of search.Type.CUSTOM_RECORD and add the ID of the custom record type (as a string). In this sample, the ID of the custom record type is 6. The custom record also includes a custom field named custrecord1.

            /**
 * @NApiVersion 2.x
 */

require(['N/search'], function(search) {
    var myCustomRecordSearch = search.create({
        type: search.Type.CUSTOM_RECORD + '6',
        title: 'My Search Title',
        columns: ['custrecord1']
    }).run().each(function(result) {
        // Process each result
        return true;
    });
}); 

          

Search for Items in a Custom List

The following sample creates a search for items in a custom list. It searches for the internal ID value of an abbreviation in a custom list named customlist_mylist.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/search'], function(search) {
    var internalId = -1;
    var myCustomListSearch = search.create({
        type: 'customlist_mylist',
        columns: [
            { name : 'internalId' },
            { name : 'abbreviation' }
        ]
    });

    myCustomListSearch.filters = [
        search.createFilter({
            name: 'formulatext',
            formula: '{abbreviation}',
            operator: search.Operator.IS,
            values: abbreviation
        });
    ]

    var resultSet = myCustomListSearch.run();
    var results = resultSet.getRange({
        start: 0,
        end: 1
    });
    for(var i in results) {
        // log.debug('Found custom list record', results[i]);
        internalId = results[i].getValue({
            name:'internalId'
        });
    };
}); 

          

Delete a Saved Search

The following sample shows how to delete a saved search.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/search'], function(search) {
    function deleteSearch() {
        search.delete({
            id: 'customsearch_my_so_search'
        });
    }

    deleteSearch();
}); 

          

Search Using a Specific Record Field

The following sample shows how to search for records using the value of the operationdisplaytext field. If you run this sample in your account, be sure to replace the placeholder value <transactionId> with a valid value from your account.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
* @NApiVersion 2.x
*/
require(['N/search'], function(search) {
    var mySearch = search.create({
        type: search.Type.TRANSACTION,
        columns: ['item', 'operationdisplaytext'],
        filters: [
            ['internalid', search.Operator.ANYOF, <transactionId>],
            'and',
            ['operationdisplaytext', search.Operator.EQUALTO, 20]]
    });

    var myResultSet = mySearch.run();
    var resultRange = myResultSet.getRange({
        start: 0,
        end: 20
    });
}); 

          

General Notices