> For the complete documentation index, see [llms.txt](https://docs.sharelogic.com/unifi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sharelogic.com/unifi/configure/administration/data-stores.md).

# Data Stores

Data stores are persistent key-value pairs linked to a record, stored in the `x_snd_eb_data_store` table and accessible via the `dataStore` API. Values are stored as strings, with methods that allow them to be retrieved as strings or automatically converted to and from objects when required.

## Usage

{% hint style="success" %}
Use Data Stores to easily make variables persistent and retrievable.
{% endhint %}

Data Stores are used primarily in [Message Scripts](/unifi/configure/message-scripts.md) and [Poll Processor](/unifi/configure/polling/poll-processors.md) scripts when you want to get and set functional data that is relevant to the integration but does not belong on the target record.

This is particularly handy in a uni-directional integration where polling the remote system is necessary. You can store data such as:

* The last time a request was made
* Identifiers that have been seen before
* Watermarks

## Model API

All Unifi model objects have data store built in. Calling these methods will read/write data stores on the record the object is representing.

{% hint style="info" %}
Not all objects available in Unifi scripts are Unifi model objects, available objects may be found on the [variables page](https://docs.sharelogic.com/unifi/configure/scripting/variables#message-script).
{% endhint %}

### getData(key \[, default\_value])

Get a string value from a data store. The default\_value parameter will be returned if no data store exists on the model with the specified key.&#x20;

```javascript
bond.getData('log_count', "0");
```

### getDataObject(key \[, default\_object]) {

Retrieve a data value and parse it as JSON to return an object. The default\_object parameter will be returned if no data store exists on the model with the specified key.

```javascript
bond.getDataObject('logs_seen');
```

### setData(key, value \[, description])

Store a key/value pair against a document. An optional description can be provided for future reference.

```javascript
bond.setData('log_count', parseInt(log_count, 10) + 1, "Number of logs seen")
```

### setDataObject(key, obj \[, description])

Encode an object as JSON and store the value against a document. An optional description can be provided for future reference.

```javascript
bond.setDataObject('logs_seen', [1375, 1399, 1748]);
```

## dataStore API

Data stores can be used with any record in ServiceNow using the Unifi dataStore API. We recommend prefixing `dataStore` with the Unifi scope `x_snd_eb` to prevent cross-scope issues, e.g. `x_snd_eb.dataStore.get(document, "DomainID")`.

### get(document, key \[, default\_value])

Get a string value from a data store. The default\_value parameter will be returned if no data store exists with the specified key.&#x20;

```javascript
x_snd_eb.dataStore.get(current, "MyStr");
```

### getAsObject(document, key \[, default\_object]) {

Retrieve a data value and parse it as JSON to return an object. The default\_object parameter will be returned if no data store exists with the specified key.

```javascript
x_snd_eb.dataStore.getAsObject(current, "MyObj");
```

### remove(document, key)

Remove a single data store.

```javascript
x_snd_eb.dataStore.remove(current, "MyStr");
```

### removeAll(document)

Remove all data stores for a document.

```javascript
x_snd_eb.dataStore.removeAll(current);
```

### set(document, key, value \[, description])

Store a key/value pair against a document. An optional description can be provided for future reference.

```javascript
x_snd_eb.dataStore.set(current, "MyStr", "My value", "This is my value");
```

### setFromObject(document, key, obj \[, description])

Encode an object as JSON and store the value against a document. An optional description can be provided for future reference.

```javascript
x_snd_eb.dataStore.setFromObject(current, "MyObj", {foo: "bar"}, "Hello world!");
```
