# Referencing a cache in mapping transformations

## Introduction

If caches have been [added to your process flow](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape) or [company-level caches have been added](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape) for use in any process flow, you can reference these in field mapping transformations.&#x20;

Using a [cache lookup transformation function](#adding-a-cache-lookup-transformation), you can look up values from a cache and map them to fields in a target system.

{% hint style="info" %}
If you've added/updated a [map shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape) before, you'll be used to selecting a **source** field and a **target** field. However, when referencing a cache we don't select a **source** field - the specified cache data is our source.
{% endhint %}

## Adding a cache lookup transformation

**Step 1**\
In your process flow, access settings for the **map shape** that you want to update:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FxRLGZBU9Bff0uJ5qWA5d%2Flookup%201.png?alt=media&#x26;token=625f473f-e190-4a8b-8a93-4c21c19329df" alt="" width="172"><figcaption></figcaption></figure></div>

**Step 2**\
Click the **add mapping rule** option - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fzs3TdSTrUdVL7ylcfxSg%2Fcache%20transform%202.jpg?alt=media&#x26;token=7d1345d2-cd5b-4663-a455-60275ba8b39f" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 3**\
Click the **add transform** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F1BB49NjjHe51NfTFA83U%2Fcache%20transform%203.jpg?alt=media&#x26;token=43d6eb82-c54b-46f3-9029-5854f58b7307" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 4**\
Click the **add transform** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FYHzFyxX2aWKMeCLIS8A0%2Fcache%20transform%204.jpg?alt=media&#x26;token=3db12198-71e4-4c2c-9ab7-5ca5305dfdc3" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 5**\
Click in the **name** field to access a list of all available transform functions, then select **cache lookup**:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FqUFe5L8BRiwv2zXT1zmS%2Fcache%20transform%205.jpg?alt=media&#x26;token=dcbbafbf-4fe1-47ed-80c4-4d657f0d1c1e" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 6**\
Cache reference fields are displayed:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fq23DKpidKm8Adinesh4s%2Fcache%20lookup%20options.png?alt=media&#x26;token=a99cd361-c9fb-4fb1-a617-e65bd9a11b7a" alt="" width="375"><figcaption></figcaption></figure></div>

Complete these fields using the table below as a guide:

<table><thead><tr><th width="269">Field</th><th>Summary</th></tr></thead><tbody><tr><td>Cache</td><td><p>Use the dropdown list to select the cache that you want to reference. Available caches will be:</p><ul><li>All <strong>flow</strong>-level caches added for this process flow</li><li>All <strong>company</strong>-level caches added from any process flow</li><li>All <strong>flow run-level</strong> caches created for this run</li></ul></td></tr><tr><td>Key</td><td>Enter the key that was specified in the <a href="add-to-cache-shape">add to cache shape</a> for the cache that you want to access here. <br><br>Alternatively, if this transformation is preceded by another transformation function, you can leave this field blank and pick up a value from the output of the previous function. For further information please see the <a href="#using-output-from-a-transform-as-the-lookup-cache-key">Using output from a transform as the lookup cache key</a> section.</td></tr><tr><td>Lookup</td><td>You can use dot notation to look up specific elements from the cached payload. If you leave this field blank, the full cached payload is retrieved.</td></tr><tr><td>Default</td><td>If required, specify a default value to be used if the cache lookup transform doesn't find a value to return.</td></tr><tr><td>Load all pages</td><td>When paginated data is pulled from a <a href="https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connection-shape">connection shape</a>, a payload is created for each page. If the <strong>save all pages</strong> option is toggled ON when a cache is created, the payload for each page is saved to its own <strong>cache key</strong> (with key names generated dynamically from a specified key and page numbers). The <strong>load all pages</strong> option here can be used if you want to lookup all of these pages.</td></tr><tr><td>Fail on miss</td><td>If this option is togged ON, the map shape (and therefore the process flow) will fail if the cache lookup can't be fulfilled. </td></tr></tbody></table>

<details>

<summary><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FuKTXJEY34CVnak6PxrOz%2Fexample%20icon%202.svg?alt=media&#x26;token=7c8af2c5-9519-4757-bea9-172569a023bd" alt="" data-size="line"> Lookup notation example</summary>

Let's look at a sample payload:

{% code lineNumbers="true" %}

```json
[
    {
        "objectId": "SI9cUU7SV2",
        "customerID": "10000006",
        "orderID": "0001",
        "first_name": "Jude",
        "last_name": "Hey",
        "productType": "musical",
        "createdAt": "2023-07-10T20:42:23.107Z",
        "updatedAt": "2023-08-09T11:47:18.790Z"
    },
    {
        "objectId": "op0wuUY8Ub",
        "first_name": "Caroline",
        "last_name": "Sweet",
        "orderID": "00002",
        "customerID": "10000005",
        "productType": "sport",
        "createdAt": "2023-07-10T20:43:25.895Z",
        "updatedAt": "2023-08-09T11:47:12.910Z"
    }
]
```

{% endcode %}

The following notation in the **lookup** field will retrieve ALL `customerID` values:

{% code lineNumbers="true" %}

```
*.customerID
```

{% endcode %}

The following notation in the **lookup** field will retrieve the FIRST `customerID` value:

{% code lineNumbers="true" %}

```
0.customerID
```

{% endcode %}

The following notation in the **lookup** field will retrieve the SECOND `customerID` value:

{% code lineNumbers="true" %}

```
1.customerID
```

{% endcode %}

</details>

**Step 7**\
Accept your changes:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FbTF6lKIUGsEoXN7hPo8E%2Flookup%203.png?alt=media&#x26;token=80e229df-51fc-4c7c-801b-0f614da4e334" alt="" width="375"><figcaption></figcaption></figure></div>

...then save the transformation:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FwxxA1mQD3zSwouhCgORQ%2Flookup%204.png?alt=media&#x26;token=e987723d-059a-4658-b7e0-895c7264c4b6" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 8**\
Now you can select a target field in the usual way. Once your mapping is complete, the row should be displayed without a source field - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FzZJO28UDbB0lLLN08Tik%2Fcache%20transform%2012.jpg?alt=media&#x26;token=55f63a8d-fc46-48ce-87e7-35eceecbd9f5" alt="" width="375"><figcaption></figcaption></figure></div>

From here you can save changes or add more mapping rules as needed. Next time the process flow runs, the specified cache values will be mapped to the target field.

## Using output from a transform as the lookup cache key

The steps detailed above show how to configure the cache lookup transform with a known cache key. However, it's possible to populate the cache key automatically, using the output from a previous transform function.

To do this, you add a mapping row in the usual way and define any required transform functions to produce the required value for cache keys. Once this is done, add a **cache lookup** transform function (as shown [above](#adding-a-cache-lookup-transformation)) but leave the `key` field blank.

When the `key` field is blank, output from the previous transform function for the mapping is applied.

### Example

Suppose you have a cache where multiple cache keys have been defined in the form:

`itemref`-`last_name`

For example:

`1000021-Smith`

Now suppose you want to define a **cache lookup** transformation which will determine the key by manipulating mapped fields. You would:

* Add a mapping row with two source fields - one for `itemref` and another for `last_name`.
* Select `itemref` as the target field.
* Add a [concatenate](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape/working-with-field-transformations/available-transform-functions/string-transform-functions/concatenate-transform-function) transform function to join `itemref` and `last_name` fields with a hyphen.
* Add a **cache lookup** transform function as defined [above](#adding-a-cache-lookup-transformation), but leave the `key` field blank
* When the process flow runs, output from the **concatenate** transform function will be applied as the `key` for the **cache lookup** transform function.

{% hint style="info" %}
The example above describes how you might use a [concatenate](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape/working-with-field-transformations/available-transform-functions/string-transform-functions/concatenate-transform-function) transform function as the means to generate a cache key however, the output from any transform function can be used.
{% endhint %}

## Related information

* [Add to cache shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape)
* [Load from cache shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/load-from-cache-shape)
* [Cache maintenance](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/cache-maintenance)
