Execute javascript
Function: Execute Javascript
This function allows you to run custom JavaScript code directly within your application. It's a powerful tool for implementing custom logic, performing calculations, or manipulating data in ways that might not be directly supported by other built-in actions. You can also capture specific results from your JavaScript code and make them available for other parts of your application's workflow.
Input
- Javascript Code: This is the block of JavaScript code you want the platform to execute. You can write standard JavaScript, use
console.log\(\)for debugging messages that appear in your application's logs, and utilize a specialaddOutput\(name, value\)function to store specific results that can be used by subsequent actions.
Output
- Output: This action produces an object (a collection of named values) that contains any data you explicitly added using the
addOutput\(name, value\)function within your JavaScript code. By default, this output object will be available under the variable name "RESULT" for use in subsequent actions. You can typically rename this output variable in your platform's action configuration if you prefer a different name.
Execution Flow
Real-Life Examples
Here are some practical examples of how you can use the "Execute Javascript" function:
Example 1: Simple Calculation and Logging
- Scenario: You want to calculate the sum of two numbers and display the result in your application's execution logs for debugging or monitoring.
- Inputs:
- Javascript Code:
let price = 50;
let taxRate = 0.08; // 8%
let totalTax = price * taxRate;
let finalPrice = price + totalTax;
console.log\("Item price: $" + price\);
console.log\("Calculated tax: $" + totalTax.toFixed\(2\)\);
console.log\("Final price including tax: $" + finalPrice.toFixed\(2\)\);
- Javascript Code:
- Result: The application's execution log will show messages like: "Item price: $50", "Calculated tax: $4.00", and "Final price including tax: $54.00". No specific output variable will be created for subsequent steps, as the
addOutputfunction was not used.
Example 2: Formatting Data and Storing Output
- Scenario: You have a user's first name and last name from a form, and you need to combine them into a full name, convert it to uppercase, and then store both the full name and its length for use in a later step (e.g., sending a personalized confirmation message).
- Inputs:
- Javascript Code:
let userFirstName = "john"; // Imagine this comes from a previous step
let userLastName = "doe"; // Imagine this comes from a previous step
let fullName = \(userFirstName + " " + userLastName\).toUpperCase\(\);
let nameLength = fullName.length;
addOutput\("formattedFullName", fullName\);
addOutput\("fullNameLength", nameLength\);
console.log\("Formatted Full Name: " + fullName\);
console.log\("Length: " + nameLength\);
- Javascript Code:
- Result: An output object will be created (by default named "RESULT"). This object will contain two properties:
formattedFullNamewith the value "JOHN DOE", andfullNameLengthwith the value 8. You can then access these values in subsequent actions, for example,\{\{RESULT.formattedFullName\}\}or\{\{RESULT.fullNameLength\}\}.
Example 3: Conditional Logic and Dynamic Output
- Scenario: You need to check a customer's loyalty points. If they have more than 500 points, they qualify for a "Gold" status and a special discount code. Otherwise, they are "Silver" status. You want to store their status and any applicable discount code.
- Inputs:
- Javascript Code:
let customerPoints = 650; // Imagine this value comes from a database lookup
let loyaltyStatus;
let discountCode = "NONE";
if \(customerPoints > 500\) \{
loyaltyStatus = "Gold";
discountCode = "GOLD20"; // 20% off
console.log\("Customer qualifies for Gold status!"\);
\} else \{
loyaltyStatus = "Silver";
console.log\("Customer has Silver status."\);
\}
addOutput\("customerLoyaltyStatus", loyaltyStatus\);
addOutput\("applicableDiscountCode", discountCode\);
- Javascript Code:
- Result: An output object (named "RESULT" by default) will be created. In this case, it will contain
customerLoyaltyStatuswith the value "Gold" andapplicableDiscountCodewith the value "GOLD20". IfcustomerPointswas 300,customerLoyaltyStatuswould be "Silver" andapplicableDiscountCodewould be "NONE". These values can then be used in subsequent actions, such as updating a customer record or sending a personalized email.