The JavaScript code tool allows you to run custom JavaScript code directly within the automation. It is designed for cases where standard tools are not sufficient, for example, when you need to perform calculations, transform data, split values, or generate custom results before passing them to the next steps.
This tool gives you the flexibility to work with data within an automation using the JavaScript logic that you define yourself.
How the Tool Works
To add the JavaScript code tool to your automation, click the "+" button in your interface. Then, select Code.
Select JavaScript and click "Continue".
First, you must configure the input data. This means creating variables, naming them, and defining the values that will be assigned to each one. These variables can be used in your code, along with any new variables you define within the script.
To add more variables, click "Add field". A new row will be created where you can specify the variable name and set its value. If necessary, you can delete any variables (fields) that you do not need.
Next, configure the output data. Here, manually specify the name and type of the variables you wish to obtain as a result. In the following steps of your automation, you will be able to use the fields you have defined in this section.
Configuring the output data is mandatory, even if you have not created any new variables in the code.
After that, write your JavaScript code using the variables you have declared or create new ones from them.
In the following steps, you will see the same fields that you configured in the Output Data section and will be able to use them throughout your automation.
If, for any reason, the code does not execute correctly, this step of the automation will be considered an error. The execution log will show the error message returned by the JavaScript engine.
Limitations and Security
JavaScript execution runs in an isolated environment.
External network requests are not allowed.
fetchis disabled.Execution errors will stop the automation step.
JavaScript Code Execution Examples
This use case is useful when you need to calculate the duration of an event, such as order processing time or call duration.
Input: dateStart, dateEnd
Output: diffMinutes (Number)
javascript
dateStart = new Date(dateStart); dateEnd = new Date(dateEnd); let difMinutes = (dateEnd – dateStart) / (1000 * 60);
Case 2. Split the datetime into separate date and time values and trim the seconds.
Use this example when a service returns a single DateTime value, but the next step requires date and time to be separate fields.
Input: dateTime (Unix timestamp)
Output: date (String), time (String)
dateTime = new Date(+dateTime); function dateEdit(arr) { return arr.map((el) => { return el < 10 ? “0” + el : el; }); } let date = dateEdit([ dateTime.getDate(), dateTime.getMonth() + 1, dateTime.getFullYear(), ]).join(“.”); let test = dateEdit([dateTime.getHours(), dateTime.getMinutes()]).join(“:”);
Case 3. Splitting a string into multiple values
This use case is useful when a field contains multiple values separated by a delimiter.
Input: value
Output: first (String), second (String)
const parts = value.trim().split(/\s+|,|\|/);
const first = parts[0];
const second = parts?.[1];
Split multiple values using a space as the delimiter.
num4 = num4.trim().split(‘ ‘);
let name = num4[0];
let familyname = num4?.[1];
let surname = num4?.[2];
Split values using a comma as the delimiter.
A = A.trim().split(“\,”);
let num1 = A[0];
let num2 = A?.[1];
Split values using the "|" delimiter.
UF_CRM_1669820909984 = UF_CRM_1669820909984.trim().split(“\|”);
let name = UF_CRM_1669820909984[0];
let surname = UF_CRM_1669820909984?.[1];
Split a full name into first and last name.
Use this example when a service returns a full name as a single string and you need to split it into separate fields (e.g., first name and last name) before passing the data to the next steps of your automation.
Input: fullName (String)
Salida:name (String), lastname (String)
let [firstName, lastName] = fullName.trim().split(" ");
let name = firstName;
let lastname = lastName;
Case 4. Parsing query parameters from a URL
Use this example to extract parameters from a URL query string.
Input: fullName (String)
Output:name (String), lastname (String)
function parseQueryParams(url) {
const queryString = url.split(“?”)[1];
if (!queryString) {
return {};
}
const queryParams = {};
const pairs = queryString.split(“&”);
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i].split(“=”);
const key = decodeURIComponent(pair[0]);
const value = decodeURIComponent(pair[1] || “”);
if (queryParams.hasOwnProperty(key)) {
if (Array.isArray(queryParams[key])) {
queryParams[key].push(value);
} else {
queryParams[key] = [queryParams[key], value];
}
} else {
queryParams[key] = value;
}
}
return queryParams;
}
const parsedGet = parseQueryParams(url);
Replace the input value with url in the last line of the code. Set parsedGet as the output value with the StringArray type.
You can modify the example and add:
let one = parsedGet[0]; let two = parsedGet[1];
You can also use these values as output variables of type String.
Case 5. Cleaning HTML tags from text
This example is useful when the content of an email or message arrives in HTML format and needs to be cleaned before further processing.
Input: html
Output: html (String)
let regexp = /<(\/?[^>]+)>|(\n)|(\s{4,})/gm;
html = html.replace(regexp, ”).trim();
Case 6. Converting datetime to Unix timestamp
Use this example when an external system requires Unix time instead of a formatted date.
Input: amo_datetime (YYYY-MM-DD HH:MM:SS)
Output: unixTime (Number)
let [date, time] = amo_datetime.split(" ");
let [year, month, day] = date.split("-").map(Number);
let [hours, minutes, seconds] = time.split(":").map(Number);
let ls_deadline_unix = new Date(year, month - 1, day, hours, minutes, seconds).getTime();
Case 7. Returning different values based on the day of the week
This example can be used to create automations with specific logic for each day. Use it when your automation needs to send different messages depending on the day of the week.
For example, you can configure the logic to send one message on Monday, another on Tuesday, a different one on Wednesday, and so on.
const messages = {
0: "Sunday message",
1: "Monday message",
2: "Tuesday message",
3: "Wednesday message",
4: "Thursday message",
5: "Friday message",
6: "Saturday message"
};
const result = messages[new Date().getDay()];
Case 8. Capitalizing the first letter of each word
Use this example to format names or text values before sending them to another application.
Input:
strOutput:
formatted(String)
function toCapitalLetter(str) {
if (!str) return str;
return str.trim().toLowerCase().split(" ").map(w => w[0].toUpperCase() + w.slice(1)).join(" ");
}
const formatted = toCapitalLetter(str);





