Execute Javascript code
Task in alpha phase
This Data Factory task is in phase. You can contact the Product-Live team at contact@product-live.com if you want more details and get an early access.
This task allows to execute Javascript code to produce a result based on the given input parameters.
Task name:
Max execution time (seconds):
Examples
json
{
"name": "code-execute-javascript",
"taskReferenceName": "get_input",
"description": "Initialize job inputs",
"inputParameters": {
"data": {
"number": 3
},
"expression": "function f() { return $.data.number % 2 === 0; } f();"
},
"type": "SUB_WORKFLOW",
"startDelay": 0,
"optional": false
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
json
{
"name": "code-execute-javascript",
"taskReferenceName": "compute_and_export",
"description": "Compute result and export as file",
"inputParameters": {
"expression": "return { sum: $.data.a + $.data.b };",
"data": { "a": 10, "b": 20 },
"response": "FILE"
},
"type": "SUB_WORKFLOW",
"startDelay": 0,
"optional": false
}1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
json
{
"name": "code-execute-javascript",
"taskReferenceName": "process_files",
"description": "Load and process external files",
"inputParameters": {
"expression": "return { firstItem: $.resources[0].content };",
"resources": [
{
"key": "config",
"file": "file://assets/config.json",
"loadAs": "JSON"
}
]
},
"type": "SUB_WORKFLOW",
"startDelay": 0,
"optional": false
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
json
{
"name": "code-execute-javascript",
"taskReferenceName": "run_script_from_file",
"description": "Load script from a file reference",
"inputParameters": {
"expressionFile": "file://assets/script.js",
"data": { "value": 42 }
},
"type": "SUB_WORKFLOW",
"startDelay": 0,
"optional": false
}1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
json
{
"name": "code-execute-javascript",
"taskReferenceName": "concat_variables",
"description": "Concatenate two Data Factory variables and return the result",
"optional": true,
"type": "SUB_WORKFLOW",
"inputParameters": {
"expressionFile": "file://assets/script.js",
"data": {
"var1": "${workflow.globalVariables.test_variable_1}",
"var2": "${workflow.globalVariables.test_variable_2}"
}
}
}
/* script.js content: */
/* let var1 = $.data.var1 || 'Hello';
let var2 = $.data.var2 || 'World';
let result = var1 + ' ' + var2;
return result; */1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
json
{
"name": "code-execute-javascript",
"taskReferenceName": "sum_product",
"description": "Simple arithmetic with log() calls for debugging",
"optional": false,
"type": "SUB_WORKFLOW",
"inputParameters": {
"expressionFile": "file://assets/sum-product.js",
"data": {
"a": 7,
"b": 6
}
}
}
/* sum-product.js content: */
/* let a = $.data.a;
let b = $.data.b;
log('Computing sum and product for a=' + a + ', b=' + b);
let sum = a + b;
let product = a * b;
log('Result: sum=' + sum + ', product=' + product);
return { sum: sum, product: product }; */1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
json
{
"name": "code-execute-javascript",
"taskReferenceName": "hello_world",
"description": "Iterate over items with per-item logging",
"optional": false,
"type": "SUB_WORKFLOW",
"inputParameters": {
"expressionFile": "file://assets/hello-world.js",
"data": {
"name": "Data Factory",
"items": [10, 20, 30, 40, 50]
}
}
}
/* hello-world.js content: */
/* let name = $.data.name || 'World';
let items = $.data.items || [];
log('Hello script started with name=' + name + ', items count=' + items.length);
let total = 0;
for (let i = 0; i < items.length; i++) {
log('Adding item[' + i + ']=' + items[i]);
total += items[i];
}
log('Total computed: ' + total);
return {
greeting: 'Hello, ' + name + '!',
sum: total,
count: items.length
}; */1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
json
{
"name": "code-execute-javascript",
"taskReferenceName": "generate_csv_file",
"description": "Generate a CSV file with progress logging",
"optional": false,
"type": "SUB_WORKFLOW",
"inputParameters": {
"expressionFile": "file://assets/generate-csv.js",
"data": {
"count": 5
},
"response": "FILE",
"outputFileName": "output.csv"
}
}
/* generate-csv.js content: */
/* let headers = 'id,name,value\n';
let count = $.data.count || 10;
let rows = '';
log('Generating CSV with ' + count + ' rows');
for (let i = 0; i < count; i++) {
let value = Math.round(Math.random() * 1000);
rows += (i + 1) + ',item_' + (i + 1) + ',' + value + '\n';
log('Row ' + (i + 1) + ' generated (value=' + value + ')');
}
log('CSV generation complete');
return headers + rows; */1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
json
{
"name": "code-execute-javascript",
"taskReferenceName": "throw_error",
"description": "Validate items and throw an error on invalid data",
"optional": true,
"type": "SUB_WORKFLOW",
"inputParameters": {
"expressionFile": "file://assets/throw-error.js",
"data": {
"items": [
{ "name": "valid", "value": 42 },
{ "name": "missing_value" }
]
}
}
}
/* throw-error.js content: */
/* log('Starting validation...');
let items = $.data.items || [];
log('Processing ' + items.length + ' items');
for (let i = 0; i < items.length; i++) {
log('Processing item ' + i + ': ' + JSON.stringify(items[i]));
if (items[i].value == null) {
log('ERROR: item ' + i + ' has no value, throwing...');
throw new Error('Item at index ' + i + ' is missing required field "value"');
}
}
log('All items processed successfully');
return { processed: items.length }; */1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
json
{
"name": "code-execute-javascript",
"taskReferenceName": "process_multiple_resources",
"description": "Load multiple files and combine their content",
"optional": false,
"type": "SUB_WORKFLOW",
"inputParameters": {
"expression": "let config = $.resources.find(r => r.key === 'config').content; let data = $.resources.find(r => r.key === 'data').content; log('Config version: ' + config.version); log('Data items: ' + data.length); return { configVersion: config.version, itemCount: data.length, items: data };",
"resources": [
{
"key": "config",
"file": "file://assets/config.json",
"loadAs": "JSON"
},
{
"key": "data",
"file": "file://assets/data.json",
"loadAs": "JSON"
}
]
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| Property | Type | Required | Description |
|---|---|---|---|
| The name (or type) of the task | |||
| The unique name of the task in your job. It is used to reference this task in the workflow. | |||
| The functional description of this task in your job. | |||
The type of the task. It must be SUB_WORKFLOW. | |||
| : | true: the job continues if there is an error on this task. false: the job fails. | ||
| Input parameters of the task. See below |
Input Parameters
WARNING
Data Factory variables (e.g. ) cannot be used inside the Javascript code itself. However, they can be passed as values in the parameter — they will be resolved at the workflow level before the script executes.
| Property | Type | Required | Description |
|---|---|---|---|
| The Javascript code to be executed inline. Either or must be provided. The code has access to (input data) and (loaded files) | |||
| File reference containing the Javascript code to execute. Either or must be provided. File must have property | |||
| The data to be used by the Javascript code (accessible via ) | |||
| Array of files to load. Each resource must have: (identifier), (file URL), (TEXT/JSON/XML, default TEXT), (default utf-8). Loaded resources are accessible via as an array with and properties | |||
| (default) or . When , the result is written to a file and contains the file reference | |||
The file output name. (ex: file.txt) |
Outputs
| Property | Type | Description |
|---|---|---|
| The result of the Javascript code execution. When is (default), contains the returned value. When is , contains a file reference with , , and | ||
| File reference containing execution logs (calls to function in the script) |
Referencing outputs in subsequent tasks
Use to access the result of a task in a subsequent task:
json
{
"name": "next-task",
"taskReferenceName": "use_result",
"inputParameters": {
"data": {
"previousResult": "${sum_product.output.result}",
"specificField": "${sum_product.output.result.sum}"
}
}
}1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Script context
The Javascript code executes in a sandboxed environment with the following global objects and functions:
Available globals
| Global | Type | Description |
|---|---|---|
| Input data passed via the parameter. Access nested properties with dot notation (e.g. ) | ||
| Array of loaded resources. Each element has a (string identifier) and (parsed content based on format) | ||
| Logs messages to the output file. Accepts multiple arguments. Useful for debugging and tracking execution progress |
Return value
The script must return a value using the keyword. The returned value becomes the output:
- When is (default): the returned value is available directly as a JSON object
- When is : the returned value is written to a file (string values are written as-is, objects are serialized to JSON)
Restrictions
- Maximum execution time: 20 seconds
- is forbidden and will cause the task to fail
- The script runs in an isolated context with no access to Node.js modules or network
Tips and best practices
- Prefer over inline for complex scripts — easier to read, maintain, and reuse across tasks
- Use on tasks that may fail without blocking the entire job (e.g. validation, optional enrichment)
- Always provide default values for input data to avoid runtime errors:
$.data.name || 'default' - Use generously during development — logs are written to a separate output file and do not affect the result
Script examples
javascript
// Read input data
const items = $.data.items || [];
const total = items.reduce((sum, item) => sum + item.price, 0);
// Return result as JSON
return { total, count: items.length };1
2
3
4
5
6
2
3
4
5
6
javascript
// Find a resource by its key
const config = $.resources.find(r => r.key === 'config');
const data = $.resources.find(r => r.key === 'data');
// Access parsed content (JSON resources are already parsed)
log('Config version: ' + config.content.version);
log('Data items: ' + data.content.length);
return {
settings: config.content,
itemCount: data.content.length
};1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
javascript
const items = $.data.items || [];
log('Starting processing of ' + items.length + ' items');
let total = 0;
for (let i = 0; i < items.length; i++) {
log('Processing item ' + i + ': value=' + items[i].value);
total += items[i].value;
}
log('Processing complete, total=' + total);
// Logs are available in the "logs" output file
return { total };1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
javascript
const items = $.data.items || [];
for (let i = 0; i < items.length; i++) {
if (items[i].value == null) {
// Throwing an error will fail the task with a clear message
throw new Error('Item at index ' + i + ' is missing required field "value"');
}
}
return { validated: items.length };1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
javascript
// When response is "FILE", return a string for text-based formats
const headers = 'id,name,value\n';
const rows = $.data.items
.map((item, i) => `${i + 1},${item.name},${item.value}`)
.join('\n');
return headers + rows;1
2
3
4
5
6
7
2
3
4
5
6
7