The N/search and N/query modules in SuiteScript provide different methods for retrieving data from NetSuite records, each with its own set of features and use cases. Here’s a comparison of the two:
N/search:
- Search-Based Retrieval: The
N/searchmodule allows developers to construct and execute saved searches, which are predefined queries saved within the NetSuite environment. - Granular Filtering and Sorting: With
N/search, developers can apply detailed filters and sorting criteria to refine search results, including filtering by field values, record types, date ranges, and custom criteria. - Access to Saved Searches: Developers can access and execute existing saved searches, making it easy to leverage pre-configured search criteria and results.
- Record-Level Access:
N/searchprovides access to individual records returned by the search, allowing developers to retrieve specific field values and perform custom logic on each record. - Synchronous Execution: Searches executed with
N/searchare synchronous, meaning that script execution waits for the search to complete before proceeding. This can impact script performance, especially for complex or large searches.
N/query:
- SQL-Like Queries: The
N/querymodule enables developers to construct SQL-like queries using SuiteQL, a SQL-inspired query language designed for querying NetSuite records. - Flexible Query Construction: With
N/query, developers have more flexibility in constructing complex queries, including support for joins, subqueries, and advanced filtering options. - Performance Optimization:
N/queryleverages NetSuite’s underlying search infrastructure, which is optimized for performance. This can result in faster query execution compared to some complex searches executed withN/search. - Asynchronous Execution: Queries executed with
N/queryare asynchronous, meaning that script execution can continue while the query is running. This can improve script performance by reducing wait times. - Governance Management:
N/queryprovides better governance management compared toN/search, as query operations consume fewer governance units and are subject to fewer rate limits.
In summary, N/search is well-suited for executing predefined searches and retrieving specific sets of records with granular control over filtering and sorting criteria. On the other hand, N/query is preferable for constructing complex queries with SQL-like syntax and optimizing performance, especially for large datasets or scenarios requiring advanced filtering and sorting logic. The choice between N/search and N/query depends on the specific requirements of the script and the complexity of the data retrieval operations.