ANS-0266 · SUITESCRIPT DEVELOPMENT

How to Convert a Comma-Separated String into an Array in NetSuite

Learn how to easily transform a single string containing multiple values separated by commas into a usable array for scripting purposes.

Short answer

To convert a comma-separated string into an array in NetSuite scripting, utilize the `split(',')` method. This function takes a string like '1,2,3,4' and divides it into individual elements based on the comma delimiter, resulting in an array such as `['1', '2', '3', '4']` that can be iterated or processed.

Scenario

A common requirement in NetSuite scripting involves processing data that is stored as a single string but represents multiple distinct values. For instance, a custom field might store a list of IDs or names as 'value1,value2,value3'. To effectively manipulate or iterate over these individual values, they must first be converted into an array data type.

Solution

To convert a comma-separated string into an array, the split() method can be applied to the string variable. This method takes a delimiter as an argument, which specifies where the string should be divided.For example, if a variable named myString holds the value '1,2,3,4', the following syntax will transform it into an array:

myString.split(',')

This operation will result in an array where each element is a substring that was originally separated by a comma. The resulting array would be ['1', '2', '3', '4'].

Expert NetSuite Support

Need help with this NetSuite issue?

SuiteScript Development consulting and configuration support

Talk to a consultant