Write JavaScript (upcoming)

Available in version 1.1.72 (not yet live).

Use the Write JavaScript building block to run custom code with the zw API. Your code runs inside the TaskBot runtime and can work with npm packages, variables and tables, state, device storage, and the browser context.

What You Can Do

Imports and Package Management

Install, import, manage, and use npm packages. Use standard import statements or zw.import(options, …) for custom options. API zw.import(), zw.packages.*

// @zw-run-locally

import dayjs from "[email protected]"; // auto-installs if needed
await zw.log("now", dayjs().toISOString());

// Example with options β€” 'isolate' scopes the package to this TaskBot only
const lodash = await zw.import({ isolate: true }, "[email protected]");
await zw.log("chunked result", lodash.chunk([1, 2, 3, 4], 2));

Reference: Imports and Package Management (upcoming)


Variables and Tables

Read from and write to variables and tables (native and Google Sheets). API zw.getRef(), zw.setRef()

const email = await zw.getRef({ ref_id: 3623, name: "Email" });
await zw.setRef({ ref_id: 3624, name: "Email copy", value: email });

Reference: Write and Read Variables and Tables


Local and Global State

Store and use values for the current run (local state) or across runs (global state). Stays on this device; never sent to ZeroWork servers. API zw.state.*, zw.globalState.*

Reference: Local and Global State (upcoming)


Device Storage

Persist key–value data on this device. Survives restarts and reinstalls. Stays on this device; never sent to ZeroWork servers. API zw.deviceStorage.*

Reference: Device Storage (upcoming)


Utilities

Add delays and write logs. API zw.delay(), zw.log(), zw.logTemp()

Reference: Utilities (upcoming)


Browser and Context

Launch, quit, and manage the active browser context and pages. API zw.browserContext.*

Reference: Browser Context (upcoming)


Metadata

Read TaskBot and agent details. API zw.getTaskbotInfo(), zw.getAgentInfo()

Reference: Metadata (upcoming)


Local vs. Browser Execution

Some methods only work when the code runs locally.

To run code locally, either enable the Run locally checkbox in the building block UI or include // @zw-run-locally anywhere in your code (top is recommended for visibility).

Example

  • Fully available in browser execution

    • Variables and tables β€” zw.getRef(), zw.setRef()

    • Device storage β€” zw.deviceStorage.*

    • Utilities β€” zw.delay(), zw.log(), zw.logTemp()

    • Metadata β€” zw.getTaskbotInfo(), zw.getAgentInfo()

  • Available in browser with limitations

    • Imports and package management β€” zw.import(), zw.packages.* Installing and managing packages is only available when running locally; using already imported packages is supported in the browser β€” see how here.

    • Local and global state β€” zw.state.*, zw.globalState.* Access to the live mutable object via *.access() is only available locally. In the browser, you can get a snapshot with *.browser.getCopy() and save state with *.browser.commit() β€” see how here. The clear method *.clear() is available in both local and browser execution.

    • Browser context β€” zw.browserContext.* Largely unavailable in browser execution. Only inspection methods zw.browserContext.getContextInfo() and zw.browserContext.getDefaults() are supported in the browser.


Using the zw APIs in No-Code Blocks

You can use the zw APIs in no-code blocks, too, either as expressions ${...} or as code blocks with return statements $${...}.

Exampl

Reference: Code in Inputs


Error Handling

Thrown errors are automatically saved to Error Reports in Run Reports (see Using Run Reports). Notifications, if enabled, then follow your error run notification settings (by email, Slack, or a custom webhook). This is a useful way to generate custom errors and get notified automatically.

Example

Resulting error report

Thrown errors can also be caught by the Try-Catch building blocks, see: Try-Catch.


Notes

  • Old API log(), delay(), setRef(), and getRef() (without the zw. prefix) still work. We recommend the new convention zw.* (e.g., zw.getRef()), but your current code will continue to run. Existing activePage and taskbotContext will also continue to work, although they're now discouraged β€” prefer zw.browserContext.getActivePage() and zw.browserContext.getContext().

Last updated

Was this helpful?