Creating a Script
Tutorial: Building Custom Logic with Capacity Scripts
Here is quick overview on building Scripts inside the Dev Platform. While the platform allows you to connect to external APIs, Scripts is where you build the precise logical processing your application needs—whether that’s simple math, data parsing, or complex list manipulation.
This tutorial provides a general overview of how to navigate and use the Scripts feature to build your own custom functions.
Prerequisites
Creating custom functional logic within this platform requires basic JavaScript knowledge. We handle the input/output infrastructure, but you write the execution code.
Step 1: Navigating to Scripts
The main dashboard (My Apps > Capacity Functions > Scripts) shows your library of custom functions.
Script Name: A friendly, descriptive name (e.g., "Sort a List of Strings," "Subtract").
Status: Confirm the script is
Activeand ready to be used.
Step 2: Defining Input Variables
Once you create a new script (or open an existing one, like "Sort a List of Strings"), your first task is to define what data the function receives. This happens in the Script Inputs section.
For each variable, you must specify:
Input Variable (Name): The exact name you will reference in your JavaScript code.
Required: Yes/No. If required, the script cannot run without it.
Test Value: A default value used when running a local test of the script.
In the example "Sort a List of Strings" (seen below):
my_list: A required input. (A list of strings like["b","aa","c"])order: An optional input, likely to define ASC or DESC sorting.
Image Reference 2: Script Configuration (Inputs & Code) This image captures the input definition area and the script editor above it.
Step 3: Writing the JavaScript
Now comes the actual logic. The core of this page is the Script editor. This is where you write standard JavaScript to process your inputs.
How to access defined inputs and outputs:
Your inputs and desired outputs are accessible via specific system methods. In the provided example:
JSON.parse(my_list);: You access input values directly using the variable name defined in Step 2 (my_list).set_output('sorted_list', coal_list);: To "send" data back from your function, you use theset_output()function. The first parameter is the Output name you will define next, and the second is the data itself.
Analysis of the Provided Example:
JavaScript
// 1. Get the list of strings (input) and parse it
let coal_list = JSON.parse(my_list);
// 2. Logic: Check the sort order (input)
if (order == "DESC") {
// Sort the list descending
coal_list.sort();
coal_list.reverse();
// 3. Set the result as an output named 'sorted_list'
set_output('sorted_list', coal_list);
} else {
// Otherwise, sort ascending and set output
set_output('sorted_list', coal_list.sort());
}
Step 4: Setting the Output Variable
After you write the code that generates your result, you must formally define the Script Output variable. This is essential so other parts of the platform know what kind of data your script returns.
Display Name: The name you will look for when using this script in other applications.
Data Type: Crucial for system compatibility (e.g., List, String, Number). The data type must match what you are sending via
set_output()in your JavaScript.Output (Value): When you run a test, the actual result appears here for verification.
In our "Sort a List of Strings" example (seen below):
We define an output named
sorted_list.The data type is specified as List.
Step 5: Testing Your Function
You don't need to save and deploy to see if your logic works. The Dev Platform includes integrated Test Script and Results sections at the bottom of the page.
Click "Test Script" (visible at the bottom of Image 2).
Verify Results: The Results panel (top of Image 3) will open, showing the raw
outputs(the JSON response containing your variables) and anerrorsfield.Inspect Logs: If your logic fails, the Errors panel (bottom of Image 3) will provide detailed error logs, allowing you to debug your JavaScript code directly in the editor.
Image Reference 3: Test Results and Output Definition This image shows a successful test where the sorted output ["aa","b","c"] confirms the logic processed the original ["b","aa","c"] inputs correctly. It also shows the corresponding Script Output definition.
