SuiteScript search.create Snippet Generator
Most SuiteScript developers write the same N/search pattern again and again: create the search, define filters and columns, page through results and read values. This generator fills that pattern from a few fields so you can paste working structure and adjust it for your record.
Fill in
N/search snippet
/**
* @NApiVersion 2.1
*/
define(['N/search', 'N/log'], (search, log) => {
const findRecords = () => {
const s = search.create({
type: 'salesorder',
filters: [
['status', 'anyof', 'SalesOrd:B']
],
columns: [
search.createColumn({ name: 'tranid' }),
search.createColumn({ name: 'entity' }),
search.createColumn({ name: 'trandate' })
]
});
const rows = [];
const paged = s.runPaged({ pageSize: 1000 });
paged.pageRanges.forEach((range) => {
const page = paged.fetch({ index: range.index });
page.data.forEach((result) => {
rows.push({
id: result.id,
tranid: result.getValue({ name: 'tranid' }),
entity: result.getText({ name: 'entity' }),
trandate: result.getValue({ name: 'trandate' })
});
});
});
log.audit('searchDone', rows.length + ' rows');
return rows;
};
return { findRecords: findRecords };
});Filter value formats depend on the field. List fields take internal ids or status codes, dates follow the user preference format, and transaction searches usually need a mainline filter. Check the field ids in the Records Browser. getText is used for column 2 on the assumption it is a list field; change it to getValue if it is not.
Skip the search syntax
Optional: see how plain-language questions over NetSuite data compare with hand-written searches.
Optional. The tool and every export work without this. No spam.
How to use the snippet
Pick the record type, enter a filter field, operator and value, then name three columns. Copy the output into a script, adjust the field ids to your account and run it in a sandbox. The runPaged loop reads results page by page, which is the safer pattern for any search that could return more than a few thousand rows.
- Confirm field ids in the Records Browser.
- Use getText for list fields when you need the display name.
- Add more filters with the AND keyword between filter arrays.
Filters and operators
A filter is an array of field, operator and value. Operators such as anyof and noneof work on list fields and take internal ids, while is, contains and the date operators work on text and dates. Combining filters needs the string AND or OR between arrays. For transaction searches, add a mainline condition if you only want header rows, otherwise each line returns as its own result.
Governance and paging
A plain search.run().each() stops after 4,000 results, while runPaged lets you walk larger sets in pages. Each page fetch consumes governance units, so pick a page size that balances calls against memory, and watch the remaining usage in long scripts. For pure reporting reads with joins, N/query and SuiteQL can be simpler than a long search definition.
Frequently Asked Questions
What is the difference between getValue and getText?
getValue returns the stored value, which for a list field is an internal id. getText returns the display name shown in the UI. Use getText when a person will read the output and getValue when you need the key.
Why do I get duplicate transaction rows?
Transaction searches return one row per line by default. Add a mainline is T filter to return one row per transaction header, or summarise columns if you need totals.
How many results can a search return?
The run().each() style stops at 4,000 results. runPaged lets you read larger result sets in pages, subject to governance, and is the usual choice for big searches.
Can I convert a saved search to code?
Yes. Load it with search.load for the same definition, or rebuild its filters and columns with search.create. Loading a saved search keeps admins in control of the definition, but changes to it can alter your script behavior.
If you would rather ask your NetSuite data a question than write a search, ERPray gives grounded answers over read-only data and shows the query behind each result.
Related Tools
SuiteQL Open Purchase Order Lines Query Template
A SuiteQL starter that lists purchase order lines with vendor, item, quantity and status filtered by date range and vendor.
NetSuiteSuiteScript 2.x Modules Reference
A searchable table of the SuiteScript 2.x N/ modules, what each one is for and the methods you reach for most.
NetSuiteNetSuite Transaction Type Codes Reference
Look up the short type code stored in transaction.type for common NetSuite transactions, with the matching record id.
NetSuiteSuiteScript Code Review Checklist
A practical review checklist for SuiteScript 2.x covering structure, governance, error handling, security and deployment.
More NetSuite tools
See all- NetSuite Saved Search Formula Generator
- NetSuite CSV Import Template Guide
- NetSuite Role Permission Matrix Worksheet
- NetSuite Inventory Costing Method Reference
- NetSuite Month End Close Checklist
- NetSuite License Cost Estimator
- NetSuite User Access Request Form Template
- NetSuite Custom Record Design Spec Template
- NetSuite SuiteScript Deployment Handover Template