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.

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:

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:

Step 6 Click the create button:

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

Step 8 Paste in the code below.

SetData script code
 <?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;
};

Step 9 Click the save and deploy button:

You don't need to do anything with the setData script right now. However, if you build a process flow 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.

Last updated

#1409: Release notes - 11.12.24

Change request updated