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 appdata fieldWhat it does
ScriptscriptPlain 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, encodeURIComponent and so on.
  • console.log, info, warn and error, which show up in the Console when you select Run in the job editor.
  • btoa and atob for base64. btoa only takes Latin-1 text, so for UTF-8 use btoa(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 with JSON.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.parse on bad input) stops the run, and it's retried. Wrap risky parsing in try/catch when 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.

  1. API Request: Get Private Data, Key erp, Response variable config_res.

  2. 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;
  3. 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.

  1. API Request: Get the store token, Get Private Data, Key catalogue_token, Response variable token_res.

  2. API Request: Get the price list, Get Private Data, Key catalogue_prices, Response variable prices_res.

  3. 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");
  4. 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/false value in the script for the next If Condition.

← All Glue step guides


Did this page help you?