Sort Object Members in SuiteScript for NetSuite Development

Sort Object Members in SuiteScript includes properties like ascending direction, case sensitivity, and sorting locale for effective data retrieval.

·3 min read·View Oracle Docs

Sorting data is a fundamental task in SuiteScript, particularly when working with queries. The query.Sort object plays a vital role in defining how query results are organized based on specific columns. This article details how to leverage the Sort object and its members to efficiently manage sorting in your SuiteScripts.

What is a query.Sort Object?

A query.Sort object is designed to apply sorting rules on query result columns defined by the query.Query or query.Component objects. It allows developers to specify multiple parameters that affect how data is presented when queries are executed.

How to Create a Sort

To create a sort, utilize the following methods:

  • Query.createSort(options): This method creates a sort based on the initial query definition.
  • Component.createSort(options): Utilize this for sorts based on join relationships created through Query.autoJoin(options) or Component.autoJoin(options).

Once you create a sort, it should be assigned as follows:

javascript
1myCustomerQuery.sort = [
2 myCustomerQuery.createSort({
3 column: myCustomerQuery.columns[0],
4 ascending: true,
5 caseSensitive: false,
6 locale: query.SortLocale.EN_US,
7 nullsLast: true
8 })
9];

Members of the Sort Object

Here’s a summary of the available members of the query.Sort object:

Member NameTypeDescription
Sort.ascendingbooleanSpecifies whether the sort direction is ascending. Default is true (ascending).
Sort.caseSensitivebooleanIndicates if sort is case sensitive. Uppercase letters precede lowercase when true.
Sort.column[query.Column] (read-only)Represents the specific column the query results are sorted by.
Sort.localestringThe locale setting influences how string values are sorted based on language and regional rules.
Sort.nullsLastbooleanDefines if null values should appear last in the sorted results.

Supported Script Types

The query.Sort object is applicable in both client and server scripts, making it versatile for various applications. This is critical for developers seeking to optimize data handling in their scripts.

Example Usage

Here’s a brief example illustrating how to define sorting in a query:

javascript
1var myCustomerQuery = query.create({
2 type: query.Type.CUSTOMER
3});
4myCustomerQuery.columns = [
5 myCustomerQuery.createColumn({
6 fieldId: 'entityid'
7 })
8];
9myCustomerQuery.sort = [
10 myCustomerQuery.createSort({
11 column: myCustomerQuery.columns[0],
12 ascending: true,
13 caseSensitive: true,
14 locale: query.SortLocale.EN_CA,
15 nullsLast: false
16 })
17];

With the modular nature of the Sort object, developers can create highly customized data retrieval methods tailored to their business logic.

Conclusion

Understanding the Sort object and its members is crucial for effective data organization in SuiteScript. By defining sort criteria clearly, developers can produce user-friendly output that meets specific data needs.

Source: This article is based on Oracle's official NetSuite documentation.

Key Takeaways

  • The query.Sort object allows precise control over the sorting of query results.
  • Key properties include ascending, caseSensitive, and locale for customization.
  • Both client and server scripts can leverage the sorting capabilities of the Sort object.

Frequently Asked Questions (4)

How do I specify if the sort should be ascending or descending in SuiteScript?
In SuiteScript, you specify the sort direction using the 'ascending' member of the 'query.Sort' object. Set it to 'true' for ascending order and 'false' for descending order.
Can I control case sensitivity when sorting query results in SuiteScript?
Yes, you can control case sensitivity using the 'caseSensitive' member of the 'query.Sort' object. When set to 'true', uppercase letters will precede lowercase letters.
Does the `query.Sort` object support locale-specific sorting in SuiteScript?
Yes, the 'query.Sort' object supports locale-specific sorting through the 'locale' member. This determines how string values are sorted based on language and regional rules.
Is the `query.Sort` object available in client scripts, server scripts, or both?
The 'query.Sort' object is available in both client and server scripts, offering flexibility for various script types in SuiteScript.
Source: Sort Object Members 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 Platform

View all Platform articles →