---
schema_version: "1"
id: 9e1e919cb79ad76e0d4e90420d7e0598aa9ef5ff5ffd4127dc03a45bdfca8890
title: Store and Retrieve Information in an Object Store Example
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/object-store-connector/1.2/object-store-to-store-and-retrieve.md
canonical_url: https://docs.mulesoft.com/object-store-connector/1.2/object-store-to-store-and-retrieve
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 85dda98149d3f321119da8eec2d9635a420d6d220643a61313e09867c740496e
description: MuleSoft Documentation Site
release: "1.2"
latest: false
---
---
product: Object Store Connector
version: 1.2
---

# Store and Retrieve Information in an Object Store Example

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The Object Store connector provides **Store** (`<os:store/>`) and **Retrieve** (`<os:retrieve/>`)
operations.

Target variables used with the Object Store connector are described in the
[Mule Runtime Target Variables document](https://docs.mulesoft.com/mule-runtime/latest/target-variables).

## Store Information from the Object Store

You can use an Object Store to store values like this:

```xml
<os:store key="state">
  <os:value>
    <![CDATA[#[
      output application/json
       ---
       {
          "id": attributes.queryParams.id,
          "timestamp": now(),
          "name": payload.name
        }
      ]]]>
  </os:value>
</os:store>
```

You can use the **Store** operation to store new values or update existing ones, depending on the
value of the `failIfPresent` parameter. When this parameter is set to `false` (the default), any
pre-existing value associated to that key is overwritten. If the parameter is set to true, then an
`OS:KEY_ALREADY_EXISTS` error is thrown instead.

Note that it is not possible to store null values. However, it is common to obtain a value by
evaluating an expression or transformation:

- Testing that the value is not null.

- Storing the value if it is present.

- Otherwise, doing nothing.

The `failOnNullValue` parameter simplifies this use case. By default (`true`), a `OS:NULL_VALUE`
error is thrown if a null value is supplied. However, when the parameter is set to `false`, a null
value causes this operation to do nothing: No error is raised, and no value is altered.

## Retrieve Information from the Object Store

The **Retrieve** operation retrieves the value for a given Object Store key. By default, the
retrieved value becomes the message payload. To keep the current payload and store the retrieved
value in a variable instead, use the `target` parameter (for example, `target="myVar"` so you can
access the value as `vars.myVar`). See
[Set Up Watermarks with an Object Store](https://docs.mulesoft.com/object-store-connector/1.2/object-store-to-watermark) for an example that uses
`target` to avoid overriding the payload.

Use the **Retrieve** operation to read previously stored data in a flow, for example, to resume
state, reuse cached values, or pass stored data to the next operation. For all parameters (including
`objectStore`, `target`, and `targetValue`), see the
[Retrieve](https://docs.mulesoft.com/object-store-connector/1.2/object-store-connector-reference#retrieve) operation in the connector reference.

You can use the key to retrieve single values:

```xml
<os:retrieve key="userId" />
```

The operation returns the stored value (or the default value, if provided). When you don’t use the
`target` parameter, that value becomes the new message payload for the rest of the flow.

You can use an operation like this one to provide default values when the key is not present in the
store:

```xml
<os:retrieve key="timestamp">
  <os:default-value>#[now()]</os:default-value>
</os:retrieve>
```

You can have default, complex structures:

```xml
<os:retrieve key="state">
  <os:default-value>
    <![CDATA[#[
     output application/json
     ---
     {
        "id": attributes.queryParams.id,
        "timestamp": now(),
        "name": payload.name
      }
    ]]]>
  </os:default-value>
</os:retrieve>
```

Note that the `defaultValue` parameter handles cases in which no value exists for a given key. If
you do not provide the parameter and the operation resolves to a null value, an OS:KEY\_NOT\_FOUND
error is thrown. Otherwise, the `defaultValue` is returned but not stored.

> [!TIP] All Object Store operations are synchronized at the key level. No other operation is able
> to access the same key on the same Object Store while the operation is running. If the runtime is
> running in cluster mode, this synchronization is also guaranteed across nodes.
