Skip to main content

Script

Runs a short script and hands its result to the rest of the flow. Reach for it to reshape data between two steps, such as filtering a list of articles, picking fields out of a response or working out a value, when no purpose-built action fits.

tip

Not a coder? Describe what you need and Esca writes the script. Click the hint over the Script field, or put the cursor in the field and press Ctrl+Enter (Windows/Linux) / ⌘+Enter (Mac), then say what the script should do. See Writing the script with Esca.

Fields​

FieldDescription
Brightspot ConnectionOptional. Pick a Brightspot connection if the script works with that CMS's content types. Left empty, the script knows this workspace's content types.
LanguageThe language the script is written in. Choosing JavaScript reveals its Script field.
ScriptThe code. It is checked for syntax errors as you type, and a note under it lists what the script can read. Choose Inline to write it here, Saved to use one from the Scripts catalog, or Runtime to take the code from a value an earlier step produced. See Where the script comes from.
Output TypeOptional. The shape of the value the script returns, so later steps can pick its fields by name. See What later steps see.

The script is needed to run, not to save: an empty Script field shows a needs-attention note rather than blocking the save, so you can lay out a flow before writing its code. At run time an empty script fails the step.

What the script can read​

Three values are available:

  • payload: the output of the step just before this one, or null when it produced nothing.
  • input: the values you mapped into the step on its Input tab, as input.fieldName. A mapping whose value resolved to nothing is null.
  • dari: Brightspot's data layer, which knows the content types of this workspace or of the connected CMS, so a script can build a record for a Content Edit to save. See Content API.

The value of the script's last expression is the step's output. console.log() writes to the step's log. For both, see The result and Logging.

For example, after a Content Query that lists articles, this script keeps the ones over a word count mapped in as minimumWords and returns their headlines:

1
// payload is the Content Query's output; input holds the values mapped on the Input tab.
2
const longReads = payload.results.filter((article) => article.values.wordCount > input.minimumWords);
3
4
// The last expression is what later steps see.
5
longReads.map((article) => article.values.headline);

Writing the script with Esca​

On an Inline script, Esca writes the Script field for you. Where a model is configured for the workspace, a hint appears over the field's label; click it, or place the cursor in the field and press Ctrl+Enter (Windows/Linux) / ⌘+Enter (Mac), to open Esca's card beside the node panel. Esca streams the result into the field as one edit, so Undo takes the whole thing back in one step. Once the field holds code, the card offers Explain, Tidy, Add comments, Warn on missing values and Check field names, which compares every field the script reads against what the automation actually provides and offers to fix a misspelled or moved one.

Esca can't help with a Saved or Runtime script, since there is no field here for it to write into.

What the shortcut does, by what is in the field
  • An empty field opens the card with starters for a first draft, such as Log what arrives? and Pull out fields.
  • Plain-language text that isn't code becomes the script's opening comment, and Esca writes the code under it.
  • A // comment on its own line asks Esca to write the code that follows it.
  • A selection, or a blank line inside existing code, opens the card asking what should go there; the lines stay highlighted while you answer.
  • Pressing the shortcut again while Esca is writing stops it and puts the field back the way it was.

Check field names reads the step before this one, the mappings on the Input tab, and, right after a Content trigger, the fields of the content types it watches. Run it from a selection to check only those lines. When the content fields can't be read, because no content types are chosen yet or a connected CMS is too old to describe them, Esca says why instead of guessing.

What later steps see​

With an Output Type declared, later steps see its fields. Without one, later steps see the fields of a value one of the script's runs returned. With neither, the output is unknown to later steps and offered whole.

To take the output shape from a run:

  1. Leave Output Type empty, and in the note under it, click Take shape from a run.
  2. Select a run.
  3. Click Done.

The note under Output Type names the run the shape came from. Later runs don't change it; take it again, or click Clear, to change it.

tip

Declare an Output Type when different runs can return different fields, such as a field only one branch of the script sets. A shape taken from one run only knows the fields that run returned.

What a shape taken from a run records

A run's shape records only field names and types, at most 50 fields and 4 levels deep, nearest the top first. A reference to a field the run didn't have isn't marked stale, since another run may have it. A value with no fields to expose, such as a single number, a list, or an object with no fields, is offered whole under payload. An object whose keys are all ids, whole numbers or dates, such as a differences map, counts as an object with no fields and is offered whole too.

Limits​

The sandbox and its ceilings are the same wherever a script runs; see Limits. Crossing one fails the step with a message naming the ceiling, as does a script with no code or an error it throws; see When a script fails.

Was this page helpful?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.