Creating Search Filters in SuiteScript: Error Handling and Best
Learn to create search filters in SuiteScript, handle errors like invalid operators, and use best practices for effective searches.
TL;DR
This article covers how to create search filters in SuiteScript, emphasizing the importance of using internal IDs rather than text values and how to handle common errors effectively.
How Do You Create Search Filters in SuiteScript?
Creating search filters in SuiteScript involves using the search.createFilter method from the N/search module. This method allows you to specify various parameters such as the name of the search field, the operator, and the values to filter by.
Important Considerations
- Use Internal IDs: When creating a filter for a list or record type field, always use the field's internal ID. Passing in text values directly will lead to errors. If you need to use the field's text value, consider using a formula with
name: 'formulatext'. - Filter Object: The return type of the
search.createFiltermethod is an object of typesearch.Filter, which encapsulates the filter properties.
Parameters Overview
When creating a filter, it's essential to provide accurate parameters. Below is a summary of the parameters for creating a filter:
| Parameter | Type | Description | Required/Optional |
|---|---|---|---|
options.name | string | Name or internal ID of the search field. | Required |
options.join | string | Join ID for the search filter. | Optional |
options.operator | string | Operator used for the search filter. | Required |
options.values | `string | Date | number |
options.formula | string | Formula used by the search filter. | Optional |
options.summary | string | Summary type for the search filter. | Optional |
Common Errors and Solutions
When working with search filters, it's crucial to handle potential errors gracefully. Here are some of the common error codes you may encounter:
| Error Code | Message | Thrown If |
|---|---|---|
SSS_INVALID_SRCH_OPERATOR | An search.Filter object contains an invalid operator or improper syntax: {1}. | options.operator parameter is not valid. |
SSS_INVALID_SRCH_SUMMARY_TYP | A search.Column object contains an invalid summary type: {1}. | options.summary parameter is not valid. |
SSS_MISSING_REQD_ARGUMENT | {1}: Missing a required argument: {2} | Required parameter is missing. |
Example Code
Here is a code sample demonstrating the creation of a search filter in SuiteScript. This example lists the first 100 employees who have a custom role:
1var result = search.create({2 type: 'employee',3 columns: ['firstname', 'lastname', 'role'],4 filters: [5 search.createFilter({6 name: 'iscustom',7 join: 'role',8 operator: search.Operator.IS,9 values: true10 })11 ]12}).run().getRange({13 start: 0,14 end: 10015});16log.debug({17 title: 'Result',18 details: result19});Key Takeaways
- Always use internal IDs for record fields when creating filters.
- Pay attention to error handling to manage incorrect filter setups efficiently.
- Utilize the
search.createFiltermethod effectively for robust search implementations.
Source: This article is based on Oracle's official NetSuite documentation.
Frequently Asked Questions (4)
Should I use text values or internal IDs when creating search filters in SuiteScript?
What should I do if I encounter an SSS_INVALID_SRCH_OPERATOR error?
Can I create a search filter without specifying the 'options.values' parameter?
Is the 'options.name' parameter required when using search.createFilter in SuiteScript?
Was this article helpful?
More in Searches
- Search for Sublist Line Fields With Dynamic Tables in NetSuite
Learn how to optimize search queries for sublist line fields in NetSuite using dynamic tables and SuiteQL.
- Filter Object Members in NetSuite SuiteScript
Gain insights into the Filter Object Members of the N/search module in NetSuite SuiteScript. Learn how to create and configure filters effectively.
- Search Column Syntax for SuiteScript 2.x in NetSuite
Search column syntax in SuiteScript 2.x allows encapsulation of fields in NetSuite searches, enhancing data retrieval capabilities.
- PagedData Object Members in NetSuite SuiteScript 2.0
PagedData provides efficient pagination for search results in SuiteScript, enabling effective handling of large data sets.
Advertising
Reach Searches Professionals
Put your product in front of NetSuite experts who work with Searches every day.
Sponsor This Category