query.create(options)

Note:

The content in this help topic pertains to SuiteScript 2.0.

Method Description

Creates a query.Query object.

Use this method to create your initial query definition. The initial query definition uses one query type. For available query types, see query.Type.

After you create the initial query definition, use Query.autoJoin(options) to create your first join. Then use Query.autoJoin(options) or Component.autoJoin(options) to create all subsequent joins.

For standard record types, the query type that you specify is validated immediately and must be one of the values in the query.Type enum. For custom record types, the query type that you specify is not validated until the query is executed using Query.run() or Query.runPaged() (or using the promise versions of these methods). If you specify a query type for a custom record type that does not exist, this method allows you to create the query and does not throw an error. However, when you execute the query, an error is thrown.

Important:

The N/query module lets you create and run queries using the SuiteAnalytics Workbook query engine. You can use the N/query module to load and delete existing queries, but you cannot save queries. You can save queries using the SuiteAnalytics Workbook interface. For more information, see Navigating SuiteAnalytics Workbook.

For more information about creating queries, see Scripting with the N/query Module.

Returns

query.Query

Supported Script Types

Client and server scripts

For more information, see SuiteScript 2.x Script Types.

Governance

None

Module

N/query Module

Sibling Module Members

N/query Module Members

Since

2018.1

Parameters

Note:

The options parameter is a JavaScript object.

Parameter

Type

Required / Optional

Description

options.type

string

required

The query type that you want to use for the initial query definition.

Use the query.Type enum to set this value (for an example, see Syntax).

When you execute query.create(options), the Query.type property is set based on this value.

Important:

The N/query module supports the same record types that are supported by the SuiteAnalytics Workbook interface. For more information, see Available Record Types.

options.columns

Object[]

optional

An array of objects to be used as query columns.

If you specify a value for this parameter, the Query.createColumn(options) method is called on each object in the array.

options.condition

Object

optional

A condition for the query.

If you specify a value for this parameter, the Query.createCondition(options) method is called on the object you specify.

options.sort

Object[]

optional

An array of objects representing sort options.

If you specify a value for this parameter, the Query.createColumn(options) and Query.createSort(options) methods are called on each object in the array.

Errors

Error Code

Thrown If

INVALID_RCRD_TYPE

The specified query type is invalid.

Note:

This error is not thrown if you specify a custom record type as the query type. Custom record types are validated when the query is executed using Query.run() or Query.runPaged() (or using the promise versions of these methods).

Syntax

Important:

The following code sample shows the syntax for this member. It is not a functional example. For a complete script example, see N/query Module Script Samples.

            // Add additional code
...
var myCustomerQuery = query.create({ type: query.Type.CUSTOMER
}); var mySalesRepJoin = myCustomerQuery.autoJoin({ fieldId: 'salesrep'
}); myCustomerQuery.columns = [ myCustomerQuery.createColumn({ fieldId: 'entityid' }), myCustomerQuery.createColumn({ fieldId: 'id' }), mySalesRepJoin.createColumn({ fieldId: 'entityid' }), mySalesRepJoin.createColumn({ fieldId: 'email' }), mySalesRepJoin.createColumn({ fieldId: 'hiredate' })
]; myCustomerQuery.sort = [ myCustomerQuery.createSort({ column: myCustomerQuery.columns[1] }), mySalesRepJoin.createSort({ column: mySalesRepJoin.columns[0], ascending: false })
]; var resultSet = myCustomerQuery.run();
...
// Add additional code 

          

Related Topics

N/query Module
SuiteScript 2.x Modules
SuiteScript 2.x

General Notices