N/util Module in SuiteScript: Type Verification Methods

The N/util module provides methods for verifying object and primitive types in SuiteScript, aiding effective script development.

·3 min read·View Oracle Docs

The N/util module allows SuiteScript 2.x developers to access various methods designed to verify object and primitive types. These methods can also be accessed via the global util object, which facilitates type checking throughout your scripts. This capability is essential for ensuring data integrity and type correctness in your applications.

Available Methods

The following table outlines the N/util module methods, their return types, supported script types, and descriptions:

NameReturn TypeSupported Script TypesDescription
util.each(iterable, callback)Object or ArrayClient and server scriptsIterates over each member in an object or array.
util.extend(receiver, contributor)ObjectClient and server scriptsCopies the properties in a source object to a destination object.
util.isArray(obj)booleanClient and server scriptsReturns true if obj is a JavaScript Array object, otherwise false.
util.isAsyncFunction(obj)booleanClient and server scriptsReturns true if obj is a JavaScript Async Function, otherwise false.
util.isBoolean(obj)booleanClient and server scriptsReturns true if obj is a JavaScript Boolean, otherwise false.
util.isDate(obj)booleanClient and server scriptsReturns true if obj is a JavaScript Date object, otherwise false.
util.isFunction(obj)booleanClient and server scriptsReturns true if obj is a JavaScript Function or Async Function, otherwise false.
util.isNumber(obj)booleanClient and server scriptsReturns true if obj is a JavaScript Number object or evaluates to a Number object, otherwise false.
util.isObject(obj)booleanClient and server scriptsReturns true if obj is a plain JavaScript object, otherwise false.
util.isRegExp(obj)booleanClient and server scriptsReturns true if obj is a JavaScript Regular Expression object, otherwise false.
util.isString(obj)booleanClient and server scriptsReturns true if obj is a JavaScript String object, otherwise false.

Practical Use Case

To illustrate how to utilize the N/util methods, consider the following example, which demonstrates how to use the util.each method to set fields on a sales order record:

suitescript
1/**
2 * @NApiVersion 2.x
3 */
4
5require(['N/record', 'N/util'], function(record, util){
6 // Create a sales order
7 var rec = record.create({
8 type: 'salesorder',
9 isDynamic: true
10 });
11 rec.setValue({
12 fieldId: 'entity',
13 value: 107
14 });
15
16 // Set up an object containing an item’s internal ID and the corresponding quantity
17 var itemList = {
18 39: 5,
19 38: 1
20 }
21
22 // Iterate through the object and set the key-value pairs on the record
23 util.each(itemList, function(quantity, itemId){ // (5, 39) and (1, 38)
24 rec.selectNewLine('item');
25 rec.setCurrentSublistValue('item','item',itemId);
26 rec.setCurrentSublistValue('item','quantity',quantity);
27 rec.commitLine('item');
28 });
29
30 var id = rec.save();
31});

In this example, the script creates a sales order and sets item quantities dynamically using the util.each method to iterate over an itemList object.

Who This Affects

  • Administrators: Manage script deployments and ensure correct usage of the N/util module in scripts.
  • Developers: Implement the N/util module methods to enhance type verification in SuiteScript scripts.

Key Takeaways

  • The N/util module is essential for type checking in SuiteScript 2.x.
  • It enables effective iteration over objects and arrays with its each method.
  • Developers can utilize various utility methods to verify data types, promoting script reliability and correctness.

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

Frequently Asked Questions (4)

Can the type verification methods in the N/util module be used in both client and server scripts?
Yes, all methods in the N/util module, including type verification, can be used in client and server scripts.
What does the util.isArray method return when checking if an object is a JavaScript Array?
The util.isArray method returns true if the object is a JavaScript Array, otherwise it returns false.
How can I iterate over an object or array in SuiteScript using the N/util module?
You can use the util.each method to iterate over each member of an object or array, applying a callback function to each item.
Are the type verification methods in the N/util module supported in SuiteScript 1.0?
The N/util module methods, including type verification, are specific to SuiteScript 2.x and are not available in SuiteScript 1.0.
Source: N/util Module 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 →