This page explains how to add a custom script for payload transformations. You will create a new script, add your code using IntelliSense, and then save changes ready for testing. Adding a new custom script can be broken down into three distinct stages:
This page walks through creating and testing a new script. We'll be adding a Javascript (node 18) script to change email addresses in a given payload to [email protected]
. If you'd like to try this yourself, our script and test payload are available below.
The maximum memory size for a custom script is 512MB
The maximum size of a custom script is 4GB
Follow the steps below:
Step 1 Log into the Patchworks dashboard and select scripts from the left-hand navigation bar.
Step 2 Select the create script button from the scripts page:
Alternatively, you can click the title of an existing script in the upper 'installed' panel, to make changes to a script that you've already added or installed.
Step 3 Add a unique name and a description for your script, then choose your preferred coding language:
Script names must be unique - you can't add two scripts with exactly the same name.
Step 4 Click the create button. The new script is saved and you're now in edit mode, ready to add and test your code:
Follow the steps below:
Step 1 Move down to the code panel.
Step 2 Add your script code (remember that you can use IntelliSense for easy code entry) - for example:
Step 3 When you're ready to test the script, click the save and deploy button:
This saves the code and deploys it to Patchworks, ready for testing/use.
Follow the steps below:
Step 1 Move across to the test panel.
Step 2 Add/paste a payload that can be used to test your script - for example:
Step 3 If your script references variables or flow variables, you can also add variables for testing:
Step 4 Click the run button:
...the script runs against your test payload and results are displayed at the end - for example:
For more detailed information about a script test, you can get logs:
Step 5 Keep testing the script until you're satisfied that it's working as required.
Any time that you make a change to the script and want to test it, always save and deploy the code first. When you run a script, it always uses the last saved version.
Once these tests are complete, you can:
When testing and debugging scripts, it can be useful to write specific information to logs after the script has finished processing.
You can do this by adding a logs
array key to a script, with required output added as strings - for example:
...is output as:
Log messages must be strings.
A maximum of 500 characters (in total) is allowed.
Use with care (especially in loops) as logs are output every time the script runs.
...current logs are displayed: