Execute javascript
Function: Execute Javascript
This function allows you to run custom JavaScript code directly within your application's backend processes. It's perfect for situations where you need to perform complex calculations, manipulate data, or apply specific business logic that isn't covered by standard actions.
Important Note: This action runs on the server, not in the user's web browser. This means you cannot use it to directly change what users see on their screen (like showing pop-up messages) or interact with browser-specific features. Its purpose is purely for backend data processing and logic.
Input
- Javascript Code Block: This is where you write your JavaScript code. You can use standard JavaScript syntax to define variables, perform operations, and implement conditional logic.
- Exporting Data: To make the results of your JavaScript code available to other parts of your workflow, you must use a special function called
addOutput\(name, value\).name: A text label (string) that you'll use to identify this piece of data later.value: The actual data (text, number, true/false, or even a collection of items) you wish to store.
- Example: If you calculate a user's role, you might write:
const userRole = "Admin"; addOutput\("USER_ROLE", userRole\);
- Exporting Data: To make the results of your JavaScript code available to other parts of your workflow, you must use a special function called
Output
- Output: This is a collection of all the key-value pairs that your JavaScript code "exported" using the
addOutput\(\)function. By default, this collection is namedRESULT, but you can specify a different name if needed. You can then use these values in subsequent steps of your workflow.
Execution Flow
Real-Life Examples
Example 1: Calculating a Discounted Price
Imagine you have an e-commerce application and want to calculate a discounted price based on a product's original price and a discount percentage.
- Inputs:
- Javascript Code Block:
const originalPrice = 100; // Assume this value comes from a previous step
const discountPercentage = 0.15; // 15% discount
const discountedPrice = originalPrice * \(1 - discountPercentage\);
addOutput\("DISCOUNTED_PRICE", discountedPrice\);
- Javascript Code Block:
- Result: The
Outputwill contain a key-value pair:DISCOUNTED_PRICE: 85. ThisDISCOUNTED_PRICEcan then be used in a subsequent action, like updating an order total.
Example 2: Validating User Input and Setting a Status
Suppose you're processing a form submission and need to check if a user's age is valid for a specific service.
- Inputs:
- Javascript Code Block:
const userAge = 25; // Assume this value comes from user input
let statusMessage = "";
if \(userAge >= 18 && userAge <= 65\) \{
statusMessage = "Eligible";
\} else \{
statusMessage = "Not Eligible";
\}
addOutput\("ELIGIBILITY_STATUS", statusMessage\);
- Javascript Code Block:
- Result: If
userAgeis 25, theOutputwill containELIGIBILITY_STATUS: "Eligible". IfuserAgewas 15, it would beELIGIBILITY_STATUS: "Not Eligible". This status can then trigger different follow-up actions (e.g., send an approval email or a rejection notice).
Example 3: Formatting a Full Name from Separate Fields
You have a user record with separate first name and last name fields, and you need a single "Full Name" field for a report or display.
- Inputs:
- Javascript Code Block:
const firstName = "Jane"; // Assume this value comes from a user record
const lastName = "Doe"; // Assume this value comes from a user record
const fullName = firstName + " " + lastName;
addOutput\("FULL_NAME", fullName\);
- Javascript Code Block:
- Result: The
Outputwill containFULL_NAME: "Jane Doe". This combined name can then be used to populate a field in a database or display on a user interface.