Detailed Explanation of N/SEARCH in NetSuite
1. Overview of N/SEARCH
The N/SEARCH module in NetSuite SuiteScript is a powerful tool for querying and retrieving records. It allows you to filter, sort, and handle large result sets effectively. It is often used to create dynamic searches, filter records based on criteria, and retrieve information from both standard and custom records.
2. Key Concepts in N/SEARCH
- Search Types: Defines which type of records you want to query (e.g., customer, sales order).
- Filters: Criteria to narrow down search results based on field values.
- Columns: The data fields you want to retrieve from the records.
- Saved Searches: You can create saved searches and load them programmatically to reuse later.
3. Common Methods in N/SEARCH
- create(options):
- Creates a new search with specified filters and columns.
- load(options):
- Loads an existing saved search by its ID.
- run():
- Executes the search and returns a result set.
- runPaged():
- Executes the search in pages, helpful for large result sets.
- each():
- Iterates over the results and processes them one by one.
4. Creating and Running a Search
- Example code to create a customer search and retrieve data for active customers.
5. Filters and Operators
- Basic Operators: Use operators like
equals,contains,greaterthan, and more to filter records. - Example of complex filtering using multiple operators.
6. Columns and Results
- Standard Columns: Define which fields from records to retrieve (e.g.,
entityid,email). - Formula Columns: Use formulas like
CASE,IF, etc., for custom data.
7. Handling Large Result Sets
- Use
runPaged()to break down large result sets into smaller pages for efficient processing.
8. Saved Search Integration
- Integrating saved searches programmatically using the
load()method.