> 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/connectors-and-instances/patchworks-connectors/descartes-peoplevox-prebuilt-connector/the-peoplevox-setdata-script.md).

# The Peoplevox setData script

## Introduction

If you're building a process flow with Peoplevox as a target connection, you must create a setData script and then - in your process flow - [apply this script immediately before the target connection](/product-documentation/connectors-and-instances/patchworks-connectors/descartes-peoplevox-prebuilt-connector/the-peoplevox-setdata-script/using-the-peoplevox-setdata-script-in-process-flows.md).

## Creating a setData script

**Step 1**\
From the Patchworks dashboard, select **scripts** from the left-hand navigation menu.

**Step 2**\
Click the **create script** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F7iNqdsVKV0rPeVnhAB5f%2Fs1.png?alt=media&amp;token=8f5f0cd4-08e5-4daf-914f-707da1266081" alt=""><figcaption></figcaption></figure></div>

**Step 3**\
In the **name** field, type the following:

```
PVX SetData
```

**Step 4**\
In the **description** field, type the following:

```
Convert flat Json payload to csv SetData format
```

**Step 5**\
Click in the **language** field and select **PHP 8.1**:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FL6JwQIRJcM9KhMvk7cAS%2Fs2.png?alt=media&amp;token=d02c1caa-14d5-4859-a9f1-dcd9929717a4" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 6**\
Click the **create** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F54r1kUZYCbGFyCvSbFc2%2Fs3.png?alt=media&amp;token=55ea6ea1-11ab-4dd7-87d5-324624d732cd" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 7**\
The script is created and opened in edit mode - select and remove the placeholder code:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FtaHajN4gKh11SicPybbQ%2Fs4.png?alt=media&amp;token=5a026e1b-ef9a-4df7-a7bd-120f4735f21f" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 8**\
Paste in the code below.

<details>

<summary><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FoTQs7eEfsfqZUPfRoiT7%2Ficon_code.svg?alt=media&amp;token=b4a6f8e5-8ebf-4ef8-ab24-a130d29b50c5" alt=""> SetData script code</summary>

{% code lineNumbers="true" %}

```php
 <?php

function handle($data) {

  $payload = json_decode($data['payload'], true);

  $headers = [];
  $lines = [];

  foreach ($payload as $payloadItem) {
    $lineHeaders = array_keys($payloadItem);
    
    if (empty($headers)) {
      $headers = $lineHeaders;
    } else {
      $keyDiff = array_diff_key($lineHeaders, $headers);

      foreach ($keyDiff as $key) {
        $headers[] = $key;
      }
    }

    $lines[] = $payloadItem;
  }

  $final = [sprintf('"%s"', implode('","', $headers))];

  foreach ($lines as $line) {

    $finalLine = [];

    foreach ($headers as $header) {
      if (array_key_exists($header, $line) && !empty($line[$header])) {
        $finalLine[] = trim(str_replace([',', "\n", "\r", '"', "'"], '', $line[$header]));
      } else {
        $finalLine[] = '';
      }
    }

    $final[] = sprintf('"%s"', implode('","', $finalLine));
  }

  $finalBody = implode("\n", $final);

  $data['payload'] = $finalBody;
  
  return $data;
};
```

{% endcode %}

</details>

**Step 9**\
Click the **save and deploy** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FlI6CsEgOYmdP8r2wFVSF%2Fs5.png?alt=media&amp;token=d05f6fbe-bc23-4a07-b20a-cfc9a7d6e890" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="info" %}
You don't need to do anything with the **setData** script right now. However, if you [build a process flow](/product-documentation/process-flows/building-process-flows.md) with Peoplevox as a **target** connection, **you must add a custom script shape and apply this setData script immediately before the target connection shape**. For more information please see: [Using the Peoplevox setData script in process flows](/product-documentation/connectors-and-instances/patchworks-connectors/descartes-peoplevox-prebuilt-connector/the-peoplevox-setdata-script/using-the-peoplevox-setdata-script-in-process-flows.md).
{% endhint %}
