Contact Log in

Execute JS

During a flow, sometimes you’ll need access to a full programming language to handle custom behaviors. Thankful provides a full Javascript VM for this purpose.

You have no disk or network access, and the VM will be killed after 1 second, which is plenty of time for most loops and workloads. Network access must be through separate Webhook steps.

This VM provides an ECMAScript 6 compatible JS. This means let, const are available for use.

console.log can be used in the VM to log output, and this output will be visible in the action event in the ticket itself. This can be extremely helpful when debugging. The output from console.log will be visible like this:

Screenshot showing the action event on a ticket

Additionally, we provide several built-in functions which extend the functionality of the VM. The functions are listed below with their descriptions.

Signature Input Output Description
tfHelper.xml2json(xml) String Object Converts a valid XML string into parsed JSON. Attributes are prefixed with an underscore “_” and enclosed text can be accessed with the key “#text”. It throws an exception if it fails.
tfHelper.json2xml(json) Stringor Object String Converts valid JSON (stringified or parsed) into a valid XML string. It throws an exception if it fails.

Exceptions can be caught in try-catch blocks.

Thankful by default does not pass in or out any memories from the VM. You can specifically pass a variable to the VM using the In Memories and Out Memories sections above and below the JS textarea, respectively. In Memories will be available as variables in the JS VM. The same memory can be both passed in and passed out, indicating that the Execute JS step is modifying the memory.

Using the Parse JSON checkbox, you can have all memories passed into the VM converted to JSON for you. Without this checkbox, you’ll need to run the following before trying access any JS object fields or arrays:

myMemory = JSON.parse(myMemory)

Note that Webhook response bodies can not be parsed as JSON, so you cannot use the Parse JSON checkbox when passing in webhook data. Manually parse the responses in the VM when appropriate.

Ask Support