Script step
Run JavaScript in a Glue job.
The Script step runs JavaScript. It's where you read the data that started the job, shape request bodies, parse responses and work out the values your If Conditions check.
Set it up
| In the app | data field | What it does |
|---|---|---|
| Script | script | Plain JavaScript. Your variables are on $. |
Anything you set on $ is there for later steps, in scripts as $.name and in other steps' fields as {{ name }}:
var sale = JSON.parse($.default);
$.sale = sale;
$.sale_id = sale.id;Set what you need on $ rather than returning a value from the script.
What you can use
$and the standard JavaScript built-ins:JSON,Math,Date,RegExp, string and array methods,parseInt,encodeURIComponentand so on.console.log,info,warnanderror, which show up in the Console when you select Run in the job editor.btoaandatobfor base64.btoaonly takes Latin-1 text, so for UTF-8 usebtoa(unescape(encodeURIComponent(s))).- Functions you define in one Script step are still there in later steps of the same run.
There's no fetch, setTimeout/setInterval or require. Use REST Request or API Request steps for HTTP calls.
How it behaves
- Incoming data and response bodies are strings. Start with
JSON.parse($.default), and parse response bodies withJSON.parse($.res.body). - 60 seconds per step. A script that runs longer is stopped with an error that starts with
halt, and the job fails. - An error fails the job. An uncaught exception (like
JSON.parseon bad input) stops the run, and it's retried. Wrap risky parsing intry/catchwhen you'd rather carry on. - Scripts run again on a retry. That's fine: they only change variables. Just make sure a script doesn't depend on something an earlier attempt left behind.
- Variables must be JSON-friendly. Strings, numbers, booleans, arrays and plain objects. The total size of a job's variables is limited (see Limits), so set big values you're done with to
null.
Example 1: Shape a sale for another system
Trigger: System Hook, Create Sale.
-
API Request: Get Private Data, Key
erp, Response variableconfig_res. -
Script: Shape the sale
var sale = JSON.parse($.default); var cfg = JSON.parse(JSON.parse($.config_res.body).erp); $.order_body = JSON.stringify({ reference: "POS-" + sale.receipt_number, store: cfg.store_codes[String(sale.store_id)] || "MAIN", total: sale.total_price, lines: (sale.lines || []).map(function (l) { return { item: l.item_id, qty: l.quantity, price: l.item_price }; }) }); $.has_total = sale.total_price > 0; -
If Condition: If the sale has a total, Condition
$.has_total === true. Add the step that sends{{ order_body }}under THEN.
Example 2: Merge two settings into one request
Trigger: Scheduled.
-
API Request: Get the store token, Get Private Data, Key
catalogue_token, Response variabletoken_res. -
API Request: Get the price list, Get Private Data, Key
catalogue_prices, Response variableprices_res. -
Script: Merge the settings
var token = JSON.parse($.token_res.body).catalogue_token.trim(); var prices = JSON.parse(JSON.parse($.prices_res.body).catalogue_prices); $.headers = JSON.stringify({ "Authorization": "Bearer " + token }); $.price_list = encodeURIComponent(prices.list || "retail"); -
REST Request: Fetch the catalogue,
GET https://api.example.com/catalogue?price_list={{ price_list }}, Headers{{ headers }}.
Tips
- Log while you build.
console.log("sale", $.sale)writes objects as JSON in the Console. - Don't log secrets. The Console shows whatever you log, including API keys.
- Keep conditions simple by working out a
true/falsevalue in the script for the next If Condition.
Updated about 23 hours ago
