Custom Code

This node gives you the ability to create custom code in JavaScript and launch it whenever this node is triggered. You can use it to manipulate and alter a selected parameter value.

For example, adding the country code to a selected phone number or a change the date of a conversation.

You can manipulate values collected by the agent during the conversation as well as information sent from a Webhook.

The AI Studio uses the JS-Interpreter package to run the JavaScript code. Find the documentation and its limitations here.

The returned value of the custom code is stored in the Output Parameter. The results should be only primitive types ( 'number', 'string', 'boolean'). You can use the same parameter as before, the value will be simply overridden.

How it works

In order to use the custom code node, you will need some knowledge of JavasScript. But don't worry, even if you don't, feel free to use some of the examples from our library below or consult with a JS developer.

Step by step example - Add 7 days to call start date

Take the example of an insurance company handling a mortgage request. Having collected all details, they want to tell their customers when they can expect to receive a response.

For the sake of our example, they usually respond after 7 days. In the custom code node, we are using JS code that will add 7 days to the original call start date.

Step 1: Get call start date parameter value. In our case, this will be a value populated in the $DATE parameter. This parameter can be either filled by receiving the value from a third-party service or by the user during the conversation.

var d = new Date($DATE);

Step 2: Increase the value of call_start_date days by 7

d.setDate(d.getDate() + 7);

Step 3: Return the value. The result of your JS code snippet will be stored in the output parameter. You can either choose a new parameter or the same one as before and override the previous value.

return d.toISOString();

If you want to go back in time and subtract some days from the given date in $DATE, just switch the + out for - .

Example Library

Feel free to copy-paste into your custom code node!

>> Date & Time

1. Get the exact point of time when the custom code node was reached

var now = new Date().toISOString();
return now;

Description:

New Date = Date of right now (time you reach the node)

The custom code node will return '2022-03-15T14:50:22.520Z'

If you want to separate the date from the exact time, see below -

2. Eliminate the exact time from “new date”

var dateWithNoTime = $ARRIVAL_DATE.split(" ")[0]
return dateWithNoTime;

Description:

If you use “New Date” as indicated above, you will get '2022-03-15T14:50:22.520Z' returned as a value, if you want to only use the date, use this code snippet.

The custom code node will return '2022-03-15’

3. Change date format

return new Date($DATE_PARAM).toDateString();

Description:

Use this code if you want to change the way that the date is being presented.

From 2022-03-16 to Wed Mar 16 2022

4. Change time format

var hour = Number($MEETING_TIME.split(':')[0])
var minute = $MEETING_TIME.split(':')[1]
var second = $MEETING_TIME.split(':')[2]
var code = "AM"
if (hour > 12) {
    hour = hour - 12
    code = "PM"
}
return hour + ":" + minute + " " + code

Description:

Use this code snippet if you want to change the way the time is being presented.

From 14:00:00 to 2:00 PM

>> Numbers

1. Eliminate special characters from a number sequence

var phone1Val = $phone1 
phone1Val = phone1Val.split("-").join(""); 
phone1Val = '1' + phone1Val; 
return phone1Val;

Description:

Alter the way we present a sequence of numbers (e.g. relevant for phone numbers)

Before > 972-58-650-3020 ($phone1)

The custom code node will return 19725865053020 ($phone1Val)

>> Others

1. Combine separate values into one parameter

var street = $streetname; 
var streetnumber = $streetnumber; 
var city = $city; var state = $state; 
address = streetname + streetnumber + ‘,’ + city + ‘,’ + state; 
return address;Counts the number of items in an array

Description:

Combine separate street name, street number, city, and state as a whole address with a comma ‘,’ in between

Before > fully separate values, e.g. from an API request > Benson Road ($streetname) 15 ($streetnumber) New York ($city) United States ($state)

The custom code node will return Beson Road 15, New York, United States

2. Count the number of items in an array

var result = $orders.length; 
return result;

Description:

The original request shows many different entries of the same category. In our example, “orders”, each holding more information like “order_Number”, “order_Status”, etc, and this custom code counts how many orders there are. Imagine the API response as follows -

{
  "orders": [
    {
      "orderNumber": "34534534245",
      "orderDate": "10-13-2020",
      "orderStatus": "Shipped",
    },
    {
      "orderNumber": "00291408",
      "orderDate": "10-08-2021",
      "orderStatus": "Waiting Allocation",
    },

The custom code node will return "2" as the value of the output parameter.

Last updated