# Data Stores

## Usage

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

Data Stores are used primarily in [Message Scripts](https://docs.sharelogic.com/unifi/configure/message-scripts) and [Poll Processor](https://docs.sharelogic.com/unifi/configure/polling/poll-processors) 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

## Built-in Examples

All Unifi model objects have data store built in. Calling these methods will read/write data stores on the record the object is representing. *Note: not all objects available in Unifi scripts are Unifi model objects.*

### Storing Strings

Use `getData` and `setData` to work with simple string-like data.

```javascript
// Store the current log count
var log_count = bond.getData('log_count');
bond.setData('log_count', parseInt(log_count, 10) + 1)
```

### Storing Objects

You can also work with objects just as easily by using `getDataObject` and `setDataObject`. These functions automatically take care of encoding the object for database storage and decoding it for JavaScript usage again.

```javascript
// Store the logs that have been seen in an array
var logs_seen = bond.getDataObject('logs_seen'); // [1375, 1399, 1748]
logs_seen.push(2140);
bond.setDataObject('logs_seen', logs_seen);
```

## 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.remove(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!");
```
