> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superoffice.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Array

> An array is a data structure that can store more than 1 value at the same time. It can be 1-dimensional like a list, 2-dimensional like a spreadsheet, 3-dimensional like a cube, or multidimensional. The length and number of dimensions are virtually unlimited.

An array is a data structure that can store more than 1 value at the same time. It can be 1-dimensional like a list, 2-dimensional like a spreadsheet, 3-dimensional like a cube, or multidimensional. The length and number of dimensions are virtually unlimited.

## Methods

## buildString

Return a string containing the contents of the array, separated by the delimiter string.

```crmscript theme={null}
String {instance}.buildString(String delimiter)
```

**Example:**

```crmscript theme={null}
String[] strings;
strings.pushBack(\"Alpha\");
strings.pushBack(\"Beta\");
strings.pushBack(\"Charlie\");
String delimitedString = nstrings.buildString(\",\");
printLine(delimitedString);
//prints Alpha,Beta,Charlie
```

## clear

Removes all array elements. Array length afterwards is 0.

```crmscript theme={null}
Void {instance}.clear()
```

**Example:**

```crmscript theme={null}
String[] userPlans;
userPlans.pushBack(\"Standard\");
userPlans.pushBack(\"Sales\");
printLine(\"Before clear length: \" + userPlans.length().toString());
userPlans.clear();
printLine(\"After clear length: \" + userPlans.length().toString());
//prints:
//Before clear length: 2
//After clear length: 0
```

## first()

Returns the first element in the array. Same as \[0].

```crmscript theme={null}
"{type} {instance}.first()"
```

**Example:**

```crmscript theme={null}
String[] userPlans;
userPlans.pushBack(\"Standard\");
userPlans.pushBack(\"Sales\");
String first = userPlans.first();
printLine(first); //prints Standard
```

## fromJsonString(String)

This function will append to the array the contents of the supplied JSON formatted string. Same functionality as fromXMLNode(XMLNode n), but it will parse the provided string directly without having to use parseJSON() yourself. Please note, this function will not clear the array, but rather append to it.

```crmscript theme={null}
Void {instance}.fromJsonString(String json)
```

**Example:**

```crmscript theme={null}
String[] strings;
strings.fromJsonString('[\"Alpha\", \"Beta\", \"Charlie\"]');
```

## insert

Adds an element at the specified index.

```crmscript theme={null}
Void {instance}.insert(Integer index, {type} value)
```

**Example:**

```crmscript theme={null}
String[5] userPlans;
userPlans.pushBack(\"Enterprise\");
userPlans.pushBack(\"Standard\");
userPlans.insert(0, \"Marketing\");
userPlans.insert(1, \"Sales\");
printLine(userPlans.toJsonString());
//prints [\"Marketing\",\"Sales\",\"Enterprise\",\"Standard\"]
```

## last

Returns the last array element, same as \[length() -1].

```crmscript theme={null}
"{type} {instance}.last()"
```

**Example:**

```crmscript theme={null}
String[] userPlans;
userPlans.pushBack(\"Standard\");
userPlans.pushBack(\"Sales\");
String last = userPlans.last();
printLine(last);
//prints Sales
```

## length

Returns the number of indexable elements.

```crmscript theme={null}
Integer {instance}.length()
```

**Example:**

```crmscript theme={null}
String[] userPlans;
userPlans.pushBack(\"Standard\");
userPlans.pushBack(\"Sales\");
Integer arrLength = userPlans.length()
printLine(\"Array length: \" + userPlans.length().toString());
//prints Array length: 2
```

## popBack

Removes and returns the last element from the array.

```crmscript theme={null}
"{type} {instance}.popBack()"
```

**Example:**

```crmscript theme={null}
String[4] userPlans;
userPlans[0] = \"Standard\";
userPlans[1] = \"Marketing\";
userPlans[2] = \"Sales\";
userPlans[3] = \"Service\";
String plan = userPlans.popBack();
printLine(plan);
//prints Service
```

## popFront

Removes and returns the first element in the array.

```crmscript theme={null}
"{type} {instance}.popFront()"
```

**Example:**

```crmscript theme={null}
String[4] userPlans;
userPlans[0] = \"Standard\";
userPlans[1] = \"Marketing\";
userPlans[2] = \"Sales\";
userPlans[3] = \"Service\";
String plan = userPlans.popFront();
printLine(plan);
//prints Standard
```

## pushBack

Appends an empty element to the end of the array.

```crmscript theme={null}
Void {instance}.pushBack()
```

**Example:**

```crmscript theme={null}
String[4] userPlans;
userPlans[0] = \"Standard\";
userPlans[1] = \"Marketing\";
userPlans[2] = \"Sales\";
userPlans[3] = \"Service\";
userPlans.pushBack();
printLine(userPlans.toJsonString());
//prints [\"Standard\",\"Marketing\",\"Sales\",\"Service\",\"\"]
```

## pushBack({type})

Appends an element value to the end of the array.

```crmscript theme={null}
Void {instance}.pushBack({type} value)
```

**Example:**

```crmscript theme={null}
String[4] userPlans;
userPlans[0] = \"Standard\";
userPlans[1] = \"Marketing\";
userPlans[2] = \"Sales\";
userPlans[3] = \"Service\";
userPlans.pushBack(\"Enterprise\");
printLine(userPlans.toJsonString());
//prints [\"Standard\",\"Marketing\",\"Sales\",\"Service\",\"Enterprise\"]
```

## pushFront

Adds an empty element of the correct type to the array.

```crmscript theme={null}
Void {instance}.pushFront()
```

**Example:**

```crmscript theme={null}
String[4] userPlans;
userPlans[0] = \"Standard\";
userPlans[1] = \"Marketing\";
userPlans[2] = \"Sales\";
userPlans[3] = \"Service\";
userPlans.pushFront();
printLine(userPlans.toJsonString());
//prints [\"\",\"Standard\",\"Marketing\",\"Sales\",\"Service\"]
```

## pushFront({type})

Adds a value to the beginning of an array.

```crmscript theme={null}
Void {instance}.pushFront({type} value)
```

**Example:**

```crmscript theme={null}
String[4] userPlans;
userPlans[0] = \"Standard\";
userPlans[1] = \"Marketing\";
userPlans[2] = \"Sales\";
userPlans[3] = \"Service\";
userPlans.pushFront(\"Enterprise\");printLine(userPlans.buildString(\",\"));
//prints Enterprise,Standard,Marketing,Sales,Service
```

## sort

This function will sort the array. It will only work on one-dimensional arrays. Arrays of basic types (Integer, String, Date, etc) will be sorted according to standard behavior. Arrays of complex types (SearchEngine, HTTP, etc) will not be sorted. Arrays of structs will be sorted if the struct has a function with the following signature: Boolean compare(sameStruct other).

```crmscript theme={null}
Void {instance}.sort()
```

**Example:**

```crmscript theme={null}
Integer[] ints;
ints.pushBack(8);
ints.pushBack(2);
ints.pushBack(4);
ints.sort();
printLine(ints.buildString(\",\"));
//prints 2,4,8
```

## toJsonString

Returns a JSON formatted string of the array's contents. Same functionality as toJson(JSONBuilder), but doesn't require a JSONbuilder.

```crmscript theme={null}
String {instance}.toJsonString()
```

**Example:**

```crmscript theme={null}
String[] strings;
strings.pushBack(\"Alpha\");
strings.pushBack(\"Beta\");
strings.pushBack(\"Charlie\");
String json = strings.toJsonString();
//prints [\"Alpha\",\"Beta\",\"Charlie\"]
```


## Related topics

- [Arrays](/en/automation/crmscript/fundamentals/arrays.md)
- [Void](/en/automation/crmscript/reference/CRMScript.Global.Void.md)
- [JSONBuilder](/en/automation/crmscript/reference/CRMScript.Native.JSONBuilder.md)
- [Running a query](/en/automation/crmscript/searchengine/se-run.md)
- [String](/en/automation/crmscript/reference/CRMScript.Global.String.md)
- [Vector](/en/automation/crmscript/reference/CRMScript.Native.Vector.md)
