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.

·3 min read·View Oracle Docs

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.createFilter method is an object of type search.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:

ParameterTypeDescriptionRequired/Optional
options.namestringName or internal ID of the search field.Required
options.joinstringJoin ID for the search filter.Optional
options.operatorstringOperator used for the search filter.Required
options.values`stringDatenumber
options.formulastringFormula used by the search filter.Optional
options.summarystringSummary 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 CodeMessageThrown If
SSS_INVALID_SRCH_OPERATORAn search.Filter object contains an invalid operator or improper syntax: {1}.options.operator parameter is not valid.
SSS_INVALID_SRCH_SUMMARY_TYPA 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:

suitescript
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: true
10 })
11 ]
12}).run().getRange({
13 start: 0,
14 end: 100
15});
16log.debug({
17 title: 'Result',
18 details: result
19});

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.createFilter method 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?
When creating search filters in SuiteScript, it's important to use internal IDs rather than text values for list or record type fields to avoid errors.
What should I do if I encounter an SSS_INVALID_SRCH_OPERATOR error?
An SSS_INVALID_SRCH_OPERATOR error indicates that your search.Filter object contains an invalid operator or improper syntax. Double-check the operator you're using in the options.operator parameter to ensure it's valid.
Can I create a search filter without specifying the 'options.values' parameter?
Yes, specifying the 'options.values' parameter is optional when creating a search filter with search.createFilter. However, depending on the operator used, you might need to provide values to get desired search results.
Is the 'options.name' parameter required when using search.createFilter in SuiteScript?
Yes, the 'options.name' parameter is required as it specifies the name or internal ID of the search field for which the filter is being created.
Source: Errors Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in Searches

View all Searches articles →