> For the complete documentation index, see [llms.txt](https://doc.wearepatchworks.com/product-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.wearepatchworks.com/product-documentation/patchworks-services/working-with-services/field-mappings-and-transformations-services/field-transformations-services/using-a-custom-script-in-field-transformations-services.md).

# Using a custom script in field transformations (services)

## Introduction

This page explains how to apply a custom script as a transform function for service field mappings.

{% hint style="danger" %}
If this is the first time you are using this script, you are strongly advised to work with a test service and ensure everything syncs as expected before applying to live services.&#x20;

In Patchworks, a test service can be created by defining **test** (rather than **live**) source and destination connectors which point to sandbox URLs for the associated systems.
{% endhint %}

## The steps

**Step 1**\
Access the [services list](/product-documentation/patchworks-services/working-with-services/the-services-list.md):

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/l7CaBjzBF62UBE9oJ4lH/services%20list%20-%20no%20alerts.png" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Please see [accessing services](/product-documentation/patchworks-services/working-with-services/accessing-services.md) if you're not sure how to get to this point.
{% endhint %}

**Step 2**\
Click on the service that you wish to update:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/SCvuO0vl79aKHyaH8l1o/select%20service%20-%20client%20admin%20-%20no%20alerts%20-%20active%20-%20cropped.png" alt=""><figcaption></figcaption></figure>

**Step 3**\
Click the **mapping** tab:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/05eou9WPd7UtEBEN47tZ/apply%20custom%20script%20to%20service%203.png" alt=""><figcaption></figcaption></figure>

...existing field mappings are displayed:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/qPPUYtTjVtLfVIi2CXrH/apply%20custom%20script%20to%20service%204.png" alt=""><figcaption></figcaption></figure>

**Step 4**\
Click the mapping entry that you wish to update with a new transform (or add a new mapping row):

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/bs9I16bA5QGgh19lNzjU/apply%20custom%20script%20to%20service%205.png" alt=""><figcaption></figcaption></figure>

**Step 5**\
Click the **edit** option associated with the expanded mapping entry:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/dF75VXf6ku7gAFRtegSm/apply%20custom%20script%20to%20service%206.png" alt=""><figcaption></figcaption></figure>

**Step 6**\
Click the **add transformation** button:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/vZ2l3gawXhZb1J8gNsGl/apply%20custom%20script%20to%20service%207.png" alt=""><figcaption></figcaption></figure>

...the **transformation editor** is displayed, with a visual representation of the source and destination points for the mapping, together with an **add new function** option to define the required transformation between these two points:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/ps65RErEQpBVjRCU1TiO/apply%20custom%20script%20to%20service%207a.png" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
A maximum of 15 custom scripts can be added to a single transform.
{% endhint %}

**Step 7**\
Click the **add new function** option:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/1oamV3K7ZBpAjWaXmvSF/apply%20custom%20script%20to%20service%208.png" alt=""><figcaption></figcaption></figure>

...**function settings** are displayed:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/l6Qe7mGXS2JQGWY1i7GG/apply%20custom%20script%20to%20service%208a.png" alt=""><figcaption></figcaption></figure>

**Step 8**\
Click in the **function name** field and select **run custom script** from the dropdown list:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/2TRmh9DrHfsBekvzReFi/function%20name%20selected.png" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
The **run custom script** option is available for all data types except **float**.
{% endhint %}

...the **transformation editor** page updates and a new **select custom script** field is displayed:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/tjPTZ3hV3BO2RYok6R91/apply%20custom%20script%20to%20service%209a.png" alt=""><figcaption></figcaption></figure>

**Step 9**\
Click in the **select custom script** field to show a list of all available custom scripts, then choose the required entry for this mapping:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/RJoRB0q8HKWmBeWmFnIF/apply%20custom%20script%20to%20service%20a.png" alt=""><figcaption></figcaption></figure>

...the **transformation editor** page updates so the code and any variable fields for the selected script are shown:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/yLRoxkeEus73oo8T93mE/function%20settings%201.png" alt=""><figcaption></figcaption></figure>

...scroll down to view the code associated with this script:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/WZcdQp8y6ESQs35GmFbm/code%20and%20settings.png" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
You can't change custom script code here in the **transformation editor**. However, options are available so you can make changes to the original custom script, quickly and easily:\
\ <img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/gONH3RAvKogdixrO5hcU/edit%20and%20refresh%20script.png" alt="" data-size="original">

Click the **manage custom script** link in the info box (see **1** above) to open the custom script in edit mode, in a new browser tab. There, you can make changes and save the script - then return to the **transformation editor** and click the **refresh** icon (see 2 above) to refresh the code.&#x20;
{% endhint %}

**Step 10**\
Update variable fields as required. Click in a variable field and choose either **custom value** (then enter a static value to the right):&#x20;

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/RUAHCUKpG4FebPaFW9Rr/apply%20custom%20script%20to%20service%20b.png" alt=""><figcaption></figcaption></figure>

...or select **source mapping field** (then select the required source mapping field to the right):

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/QxW46N9uWLERBq7R8tI3/custom%20value%20entry.png" alt=""><figcaption></figcaption></figure>

**Step 11**\
Click the **save function** button:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/IAr4YhMgk6nxgUuXORVA/save%20transform.png" alt=""><figcaption></figcaption></figure>

...the **transformation editor** updates and your new function is displayed in the visual representation. From here, you can choose to add another function, or **save transformation**:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/W82hBnY44DD4any6QdhY/apply%20custom%20script%20to%20service%20d.png" alt=""><figcaption><p><br></p></figcaption></figure>

...you are returned to the mapping page, which now shows a transformation for the row you've been working on:

<figure><img src="https://content.gitbook.com/content/5mpqoXPMbUuPa8JpCLWe/blobs/UCoFLdLk8iQPqjeRdc4q/apply%20custom%20script%20to%20service%20e.png" alt=""><figcaption></figcaption></figure>

**Step 12**\
Repeat these steps as required for any other mapping entries.&#x20;
