Skip to main content
Sandboxed JS. Receives input and readonly settings. Return a JSON-serializable value. Use console.log for debug output. A script runs for at most 60 seconds and uses at most 128 MB of memory. Past either limit it stops, and the error says which limit; running it again on the same input hits the limit again. Source is not filled in from placeholders. Text like {{Name}} stays as written. Inside a Loop, item is the current element and index is the step number, starting at 0. input is still the previous node’s result.

Input

Inspector Settings. Empty-field rules for the Graph: Previous nodes. This inspector has no Insert value. The script’s input is the previous Result unless the Agent passes input. A File id replaces that: the host loads the sheet grid (grid, merges, row and column counts) and the file (input.file.artifactId, input.file.name), and ignores input. The full grid is not in the tool result. Use Sheet map to see the shape first. An agent runs one Code call at a time on an Instance. A Code step on the graph does not wait for that. When the platform is busy, a call waits in line for up to two minutes; after that, the error says the sandbox is busy. Retry that call.

Read State

Each collection in Collections this script may read is read before the script starts. The script gets state.<collection> as an array of { key, data }, sorted by key. Use it to look up records the script did not load from the file, for example a port list to match names against. All listed collections together may hold at most 20,000 records. Over that, the call fails before the script runs.

Run test

Test input (JSON or text) and Run test execute against the selected Instance. Select an Instance to test code. Output shows Result and Console. Failures show Code failed or Code test failed. View results on the node also has a Console for console.log during a Graph Run.

Output

On the next node, Previous nodes lists Code: Result ({{Code.text}} while the name is unique).

Save from code

Leave Collections this script may save empty and the return value is the Result. State is not changed. When the list is not empty, the script returns { writes: [{ collection, key, data, row }], skipped: [{ row, reason }], deletes: [...] }. Only those collection names are accepted. A duplicate key fails the call, and the error names both rows. Build the key from every field that tells the records apart. data must match the collection schema. The same key on a later run replaces that record. Key rules: State. When File id loads a sheet, row is the 1-based sheet row and is required on every write and skip. Every nonempty row (inside Rows, when set) must be written or skipped, or the call fails and lists the missing rows. With a loaded sheet, key is optional. A write without it gets <file id>:<sheet>:<row>:<n>. sheet is that sheet’s number in the file, starting at 1, the same order as the outline. It is not the sheet name: a name can contain characters a key cannot store, and removing them can turn two sheets into one key. n counts that collection’s writes without a key on the same row, so { collection: "rates", row: 12, data } twice on sheet 3 becomes …:3:12:1 and …:3:12:2. Running the same script on the same sheet again replaces those records. Another sheet of the same file does not. Pass your own key only when a later file must update the same record. If a changed script writes fewer records for a row, the extra ones from that sheet stay until the next file replaces it. deletes is optional. Each item is { collection, key } or { collection, where, exceptArtifactId }. where uses the same filters as reading State and must not be empty when you pass it. The host runs the deletes, then the writes, in one transaction. Either all of it is saved, or nothing is.

Replace an older file

A record written from a sheet that File id loaded remembers that file. Reading State shows it as sourceArtifactId. Import from a file sets it too. Saving the record another way keeps it. exceptArtifactId keeps the records last written from that file and deletes the rest that match where. Without where, it applies to the whole collection. The id must be a file on this Instance. This is how a new price list replaces the old one of the same carrier, while rows the new file does not have go away:
With Rows, repeat the same delete on every part of the sheet. The parts already saved come from this file, so they stay. dryRun true (the agent passes it) checks everything and saves nothing. The Result is JSON text: