Understanding query.create(options) in NetSuite SuiteScript 2.x

query.create(options) is a method provided by the N/query module in SuiteScript 2.x, introduced in NetSuite 2018.1, that allows developers to create a query.Query object. This method serves as the foundation for defining queries in NetSuite, enabling developers to retrieve data programmatically.

Purpose: This method initializes a query object with the specified query type. It can include columns, conditions, and sorting options for detailed query configurations.

Usage: Used in both client and server-side scripts to create and execute SuiteAnalytics queries programmatically.

Validation:

  • Standard record types are validated immediately.
  • Custom record types are validated only when the query is executed using Query.run() or Query.runPaged()

Method Overview

  • Returns: A query.Query object representing the initialized query.
  • Supported Script Types: Works in both client scripts and server scripts.
  • Governance: No governance limit is enforced for this method.
  • Module: Part of the N/query module.
  • Sibling Module Members: N/query Module Members.
  • Introduced In: NetSuite 2018.1.

Parameters

The options parameter is a JavaScript object with the following properties:

  1. options.type (string): Required.
  • Specifies the query type for the initial query definition.
  • Use the query.Type enumeration to set this value.
  1. options.columns (Object[]): Optional.
  • Represents an array of objects to be used as query columns.
  • If included, this parameter should consist of objects created with the query.createColumn(options) method.
  1. options.condition (Object): Optional.
  • Represents the condition for the query.
  • Should be an object created using the query.createCondition(options) method.
  1. options.sort (Object[]): Optional.
  • Represents sorting options for the query.
  • Each sorting object is created using the query.createSort(options) method.

Sample 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();

Leave a comment

Your email address will not be published. Required fields are marked *