> ## 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.

# Map data type

A map is a collection of key-value pairs. Both the key and the value are strings.

The elements in a map are automatically sorted on their keys.

## Constructors

### Map Map()

Creates an empty map.

### Map Map(String value)

Pass a `String` containing key-value pairs separated by "\n":

"key=value\nkey=value\nkey=value,..."

```crmscript theme={null}
Map m = Map("roses = red\nviolets = blue");
```

<Note>
  Don't add space between \n and the 1st character of the key!
</Note>

## Adding elements to maps

### Map insert(String key, String value)

`insert()` adds a new key-value pair to the map.

```crmscript theme={null}
Map m;
m.insert("Super", "Office");
```

## Accessing elements in maps

### Bool exists(String key)

`exists()` checks if the map contains the given key.

```crmscript! theme={null}
Map m = Map("height = 25\nwidth = 10\ndepth = 7");
String key = "height";

printLine(m.exists(key).toString());
```

### Integer size()

`size()` counts the elements in the map and returns that number.

```crmscript theme={null}
Map m = Map("height=25\nwidth=10\ndepth=7");

printLine(m.size().toString());
```

An empty map has size == 0.

```crmscript! theme={null}
Map m;

if (m.size() == 0) {
  printLine("This map is empty");
}
```

### String get(String key)

Returns the value for the given key.

```crmscript theme={null}
Map m = Map("height=25\nwidth=10");
String key = depth;

printLine(m.get(key));
```

`getKey()` and `getVal()` also retrieve data from the map. These methods depend on the internal iterator of the map and are described in the *Looping* section.

### String getWithFallback(String key, String fallback)

Returns the fallback value if key does not exist.

```crmscript! theme={null}
Map m = Map("height=25\nwidth=10");

printLine(m.getWithFallback("foo", "bar"));
```

## Updating elements

When working with numeric strings, you can increment values stored in the map. The increment can be given as either Integer or Float.

In both cases, you provide the key to look up the element and the **value to add** to the currently stored value.

### Void increaseValueForKey(String key, Integer value)

```crmscript theme={null}
Map m = Map("height=25\nwidth=10");
m.increaseValueForKey("height", -5);
```

### Void increaseValueForKey(String key, Float value)

```crmscript theme={null}
Map m = Map("height=25\nwidth=10");
m.increaseValueForKey("width", 2.5);
```

## Removing elements

You can remove either a specific element identified by its key, or you can remove all elements.

### Void remove(String key)

Removes the element with the given key.

```crmscript theme={null}
Map m = Map("roses = red\nviolets = blue");
m.remove("violets");
```

### Void clear()

Removes **all** elements from the map.

```crmscript theme={null}
Map m = Map("roses = red\nviolets = blue");
m.clear();
```

## Looping maps

Looping (or iterating) maps is similar to looping an array. However, where you would declare and update the Integer iterator for the array index, Maps have a built-in iterator that you **position**.

<Note>
  `insert()` resets the map iterator when called.
</Note>

### Bool eof()

Returns true if the map iterator has moved past the end of the map, otherwise false.

```crmscript theme={null}
Map m;

if (m.eof()) {
  printLine("You have reached the final frontier");
}
```

<Note>
  That `eof()` returns true is **not** the same as the map is empty. It can be, but it doesn't have to be. Use `size()` to check if the map is truly empty.
</Note>

### Bool first()

Rewinds the internal iterator to the 1st element.

`first()` returns false if the map is empty.

```crmscript theme={null}
Map m = Map("height=25\nwidth=10");
m.first();
```

### Bool next()

Moves the map iterator to next position.

`next()` returns false if eof().

```crmscript theme={null}
Map m = Map("height=25\nwidth=10");
m.next();
```

### String getKey()

Returns the key pointed to by the map iterator.

```crmscript! theme={null}
Map m = Map("height=25\nwidth=10");
printLine(m.getKey());
```

### String getVal()

Returns the value pointed to by the map iterator.

```crmscript! theme={null}
Map m = Map("height=25\nwidth=10");
printLine(m.getVal());
```

### Putting it together

```crmscript! theme={null}
Map m = Map("height=25\nwidth=10");

m.first();
while (!m.eof()){
  printLine(m.getKey() + " = " + m.getVal());
  m.next();
}
```

## JSON

You can convert both ways between Maps and JSON.

**Format:**

\{"key": "value", "foo": "bar"}

### String toJson()

Converts the Map to JSON.

```crmscript! theme={null}
Map m = Map("height=25\nwidth=10");
printLine(m.toJson());
```

### Void fromJson(String json)

Converts a JSON string to a map.

```crmscript theme={null}
String s = "{"depth":"7","height":"20","width":"12.500000"}";
Map m;
m.fromJson(s);
```


## Related topics

- [SuperOffice.WebApi.Data.UserPreferenceStrings.DocTemplateTypeMap](/en/api/reference/webapi/SuperOffice.WebApi.Data.UserPreferenceStrings.DocTemplateTypeMap.md)
- [SuperOffice.WebApi.Data.ErpSyncActorTypeMapping](/en/api/reference/webapi/SuperOffice.WebApi.Data.ErpSyncActorTypeMapping.md)
- [SuperOffice.WebApi.Data.ErpSync SaveActorTypeMappingRequest](/en/api/reference/webapi/SuperOffice.WebApi.Data.ErpSync_SaveActorTypeMappingRequest.md)
- [SuperOffice.WebApi.Data.ErpSync GetActorTypeMappingRequest](/en/api/reference/webapi/SuperOffice.WebApi.Data.ErpSync_GetActorTypeMappingRequest.md)
- [SuperOffice.WebApi.Data.CRMScriptSourceMap](/en/api/reference/webapi/SuperOffice.WebApi.Data.CRMScriptSourceMap.md)
- [Map](/en/automation/crmscript/reference/CRMScript.Native.Map.md)
