Scripting with the N/query Module

The N/query module lets you create and run queries using the SuiteAnalytics Workbook query engine. Before you start creating your queries, you should be familiar with the module objects and how to use them, as well as some of the terminology used in the N/query module. You can also take a look at a script walkthrough that explains how to create queries using different approaches.

N/query Module Objects

The N/query module includes the following objects:

Query and Component Objects

The query.Query object and the query.Component object are the primary building blocks for a query created with the N/query module. Each query creates one query.Query object and one or more query.Component objects. The query.Query object encapsulates the query definition, and the query.Component object encapsulates one component of the query definition.

To create a query with the N/query module:

  1. Use the query.create(options) method to create your initial query definition (the query.Query object). The initial query definition uses one search type. For available search types, see query.Type.

  2. After you create the initial query definition, use Query.autoJoin(options), Query.joinFrom(options), or Query.joinTo(options) to create your first join.

  3. Use any of the following methods to create subsequent joins:

The query definition always contains at least one query.Component object. Each new component is created as a child of the previous component, and all components exist as children of the query definition. You can think of a component as a building block; each new component builds on the previous component created. The last component created encapsulates the relationship between it and all of its parent components.

Queries with joins contain multiple components. The query definition contains a child query.Component object for each of the following:

Condition Object

A condition narrows the query results. The query.Condition object performs the same function as the search.Filter object in the N/search Module. The primary difference is that query.Condition objects can contain other query.Condition objects.

To create conditions:

RelativeDate Object

The query.RelativeDate object represents a date that is relative to the current date. You can use relative dates when you create query conditions.

To create relative dates:

Column Object

The query.Column object is the equivalent of the search.Column object in the N/search Module. The query.Column object describes the field types (columns) that are displayed from the query results.

To create columns:

Sort Object

The query.Sort object describes how query results are sorted (for example, ascending or descending, case sensitive or case insensitive, and so on).

To create a sort:

ResultSet and Result Objects

When you are ready to execute your query, call Query.run(). This method returns a query.ResultSet object, which encapsulates the metadata for the set of results returned by the query.

To access your query results, iterate through the ResultSet.results array. Each member of the ResultSet.results array is a query.Result object. The query.Result object encapsulates a single row of the result set.

Page, PagedData, and PageRange Objects

You also can execute your query by calling Query.runPaged(). This method returns a query.PagedData object, which encapsulates a set of paged query results.

To access your query results, iterate through the paged query results using PagedData.iterator(). You can access each page of the query results, which are represented by query.Page objects. The query.PageRange object encapsulates the range of query results for a page.

N/query Module Terminology

Term

Definition

For More Information

Aggregate function

An aggregate function performs a calculation on a column of values and returns a single value. You can add aggregate functions to conditions and query results columns.

Column

A column describes the field types (columns) that are displayed from the query results. A column is also known as a query results column.

Component

When you script queries with the N/query module, your query is made up of one or more components, which are represented as query.Component objects. You can think of a component as a building block; each new component builds on the previous component created.

  • The first component created represents the initial search type and is a child of query.Query.

  • Each subsequent component created is a child of the previous component.

  • The last component created encapsulates the join relationship between it and all of its parent components.

A query always contains at least one component: the root component. When you create the initial query definition using query.create(options), the root component is created automatically. Queries with joins contain multiple components. A new component is created each time you create a join using one of the following methods:

Condition

A condition narrows the query results.

Formula

Formulas can be used to create conditions and columns.

Group

You can summarize your query results into unique groups of column values.

Join

A join lets you create a query based on a field type that is shared between two record types. You can use Query.autoJoin(options) and Component.autoJoin(options) to create a join relationship automatically based on a field that you specify. You can use Query.joinFrom(options)/Query.joinTo(options) and Component.joinFrom(options)/Component.joinTo(options) to create explicit directional join relationships from one component to another.

Page

A page represents one page from a set of paged query results. When you create a query with the N/query module, you can return the results as one result set or a set of paged results.

Paged data

Paged data represents a set of paged query results.

Page range

A page range is a set of pages from a set of paged query results.

Relative date

A relative date is a date that is relative to the current date. You can use relative dates when you create query conditions.

Result

A result is a single row from a result set.

Result set

A result set is a set of query results.

Query definition

The query definition is the initial search type you define, plus any subsequent joins you define. The initial query definition is created with query.create(options).

Query type

The query type is the initial query type of your query definition. It represents the record type you want to query for. It is set with the query.Type enum during the execution of query.create(options). For example, if you want to query for customer records, specify query.Type.CUSTOMER as the query type when you call query.create(options).

Sort

A sort is placed on a query results column to describe how the query results are sorted (for example, ascending or descending, case sensitive or case insensitive, and so on).

General Notices