The N/search module lets you create and run on-demand searches or run existing saved searches from SuiteScript. It is one of the most used modules in NetSuite development and is far more governance-efficient than loading individual records in a loop.
Creating and Running a Search
define(['N/search'], (search) => {
const mySearch = search.create({
type: search.Type.CUSTOMER,
filters: [
['isinactive', 'is', 'F']
],
columns: [
search.createColumn({ name: 'entityid', label: 'Name' }),
search.createColumn({ name: 'email' }),
search.createColumn({ name: 'phone' })
]
});
mySearch.run().each((result) => {
const name = result.getValue({ name: 'entityid' });
const email = result.getValue({ name: 'email' });
log.debug(name, email);
return true; // continue iterating
});
return {};
});
Loading a Saved Search
const savedSearch = search.load({ id: 'customsearch_my_search' });
savedSearch.run().each((result) => {
log.debug('Result', result.id);
return true;
});
Paginating Large Result Sets
For large result sets (over 4,000 results), use runPaged() instead of run() to process results in pages of up to 1,000 records at a time.
const pagedData = mySearch.runPaged({ pageSize: 1000 });
pagedData.pageRanges.forEach((pageRange) => {
const page = pagedData.fetch({ index: pageRange.index });
page.data.forEach((result) => {
log.debug('ID', result.id);
});
});
Common Filter Operators
| Operator | Description |
|---|---|
is | Exact match |
isnot | Not equal |
contains | String contains |
startswith | String starts with |
greaterthan | Greater than (numeric/date) |
lessthan | Less than (numeric/date) |
anyof | Matches any of a list of values |
noneof | Does not match any of a list |
isempty | Field is empty/null |
isnotempty | Field has a value |
Key Methods Reference
| Method | Description |
|---|---|
search.create(options) | Create a new search object |
search.load(options) | Load an existing saved search |
search.createFilter(options) | Create a filter object |
search.createColumn(options) | Create a column object |
searchObj.run() | Run the search (max 4,000 results) |
searchObj.runPaged(options) | Run with pagination (up to 1,000/page) |
searchObj.save() | Save the search as a saved search |
result.getValue(options) | Get a column value from a result row |
result.getText(options) | Get the display text of a list/select field |