To create a search in SuiteScript 2.0, you will need to use the N/search API, which is similar to the nlapiCreateSearch API in the 1.0 version.
To begin, you can use the search.create method to build your search object or use search.load to load a saved search.
Once you have your search object, you can invoke the run method to execute the search.
After running the search, you have two options for processing the results:
1. Use the each method and a callback function: This allows you to iterate over each result and perform actions on them.
2. Use the getRange method to retrieve a specific number of results: This is useful when you only need a limited number of results.
In the code example provided, we have imported the N/search module as 's' and demonstrated the usage of the each method.
Here is an example of how to create a search for finding customers:
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) {
// Perform actions on each customer search result
// Return true to continue iterating, false to stop
return true;
}
By following these steps and customizing the filters and columns based on your specific requirements, you can successfully create and execute searches using SuiteScript 2.0.