# 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).&#x20;

## Creating a setData script

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

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

<div align="left"><figure><img src="/files/ubsaABIuJ4FxrQjNPoLg" 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="/files/KhQkwpDhOa6sg8Ez5dBs" alt="" width="563"><figcaption></figcaption></figure></div>

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

<div align="left"><figure><img src="/files/EqU6mUFybFSrHQJhvSh9" 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="/files/ZjjxYYtKJoIgPxddKKJ8" alt="" width="563"><figcaption></figcaption></figure></div>

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

<details>

<summary><img src="/files/4iYjBn9HEfUXX4ZSP1vh" 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="/files/lQ8XoFDSYwUD2Bm2MgvB" 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.wearepatchworks.com/product-documentation/connectors-and-instances/patchworks-connectors/descartes-peoplevox-prebuilt-connector/the-peoplevox-setdata-script.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
