javascriptjsonsearchnetsuitesuitescript

How to create a Search in SuiteScript 2.0 version


I want to create an search for the record using "SuitScript 2.0 version" . I know that i can achieve it using "SuiteScript 1.0" using nlapiSearchRecord() api using filters and conditions but i want do this with SuitScript 2.0 version. To this in "SuiteScript 2.0 " the "N/search Module" have to be used but not getting how to did the search in 2.0 in equivalent to suitscript 1.0 version.

Could any one give an example for the search in SuiteScript 2.0 version.

Thanks in Advance.


Solution

  • You are correct that you will use N/search. It uses an analogous API to the 1.0 API of nlapiCreateSearch.

    You will use search.create to build out your search object or search.load to load a saved search. Then you will invoke run on the resulting search object. Finally, you can process the results in two ways:

    1. Use the each method and a callback
    2. Use the getRange method to get a specific number of results

    In the example below, I've imported N/search into my module as s and shown the usage of the each method.

    function findCustomers() {
        // Create and run search
        s.create({
            "type": "customer",
            "filters": [
                ['isinactive', s.Operator.IS, 'F'], 'and',
                ['company', s.Operator.NONEOF, ['123','456']
            ],
            "columns": ['email', 'firstname', 'lastname']
        }).run().each(processCustomer);
    }
    
    function processCustomer(result) {
        // do something with Customer search result
        // returns a boolean; true to continue iterating, false to stop
        return true;
    }