Use data from earlier steps in a code step

A code step can read the output of the steps that run before it in your flow, such as the fields an extraction model found or the result of an action. You refer to a step by its name with $("<step name>"), and the code editor suggests the step names and their fields as you type.

This article explains how to reference earlier steps in your code. For an introduction to code steps, see Use a code step in an automation.

Reference a step by its name

Inside your code, $("<step name>") returns the output of that step. Use the name the step has on the canvas.

For example, if your flow has an extraction step called Extract order details, this code reads the customer number and PO number it extracted and saves a reference as output:

import tekst from "tekst";

async function run() {
  const order = $("Extract order details").extraction;

  tekst.dump({
    reference: `${order.customer_number}-${order.po_number}`,
    lineCount: order.order_lines.length,
  });
}

Code editor with a code step that reads the output of the Extract order details step

You can reference the trigger and every step that always runs before the code step. Steps that come after the code step, or that sit on a branch that does not always lead to it, are not available.

Let the editor suggest steps and fields

You do not need to remember step names or field names. The code editor in the side panel and the larger editor behind Open in code editor both suggest them:

  1. Type $(". The editor lists the steps you can reference. Pick one with the arrow keys and press Enter.
  2. Move past the closing parenthesis and type a dot. The editor lists the fields that step outputs. Select a field to see its type, such as text, number, or a list. Type another dot to go into a nested field.

Code editor suggesting the steps Extract order details and Trigger after typing $(

Code editor suggesting the fields customer_number, delivery_date, order_lines and po_number of the Extract order details step

Which fields the editor knows

  • Steps with a fixed output, such as the trigger, model steps and actions, show their fields right away. For an extraction step, the extracted fields sit under extraction.
  • Apps and other code steps have no fixed output. The editor learns their fields from the last test run, or from the run you selected in the monitor. Run a test first to get suggestions for these steps. Your code can read their output before that; the editor just cannot suggest the fields yet.

Good to know

  • Renaming a step does not break your code. Tekst saves the reference with the step's internal ID, and the editor shows the step's current name. After a rename, the new name appears in your code automatically.
  • Steps with the same name get a number. If two steps above the code step are both called "Add labels", the second one is $("Add labels (2)"). Give your steps distinct names to keep your code easy to read.
  • A step you just added appears in the suggestions once the builder has saved your changes, which it does automatically.
  • A name that matches no step above the code step is underlined, and the side panel warns that the step will fail when it runs. Check the spelling, and check that the step comes before the code step in the flow.

Code step side panel warning that $(

Test your code

Run a test to check that your code reads the values you expect. Code steps are skipped in Safe test mode, so choose Full, or Custom with the code step selected. After the test, select the code step to check its output and logs. The test also gives the editor the fields of any apps and code steps above this one.

Related articles

Was this article helpful?

0 out of 0 found this helpful

0 comments

Please sign in to leave a comment.