Logic

Custom code

Custom code runs your own JavaScript, for what the other blocks cannot do: change, add or drop fields, filter the items, or reshape the whole list. Write the body of a function that gets item (once for each item) or items (the whole list), plus the flow’s variables and notes. It runs on the server in a sandbox with no access to the web or files.

In the editor: Your own JavaScript, for what the other blocks cannot do: change, add or drop fields, filter and reshape the items. It runs on the server in a sandbox with no access to the web, files or anything else, 3 seconds of CPU and 32 MB at most, on plain data in and out. A test run runs it too.

Settings

Run it

Pick one

Whether your code runs once for each item, or once with the whole list.

Your code gets item (one at a time) and returns it, changed as you like; return nothing to drop it.

  • For each item Your code gets item. Return it to keep it; return nothing (or null or false) to drop it. items, the whole list, is there too, to look things up.
  • On the whole list Your code gets items and returns a list: fewer, more, sorted, grouped, merged.

Code

A setting

The body of a function. With For each item, return the item (changed as you like) or nothing to drop it. On the whole list, return the new list.

The body of a function. Available: item or items, vars (the flow’s variables), notes (run values such as a report). Values from a table or a sheet are text: use Number(item.x) to add them up. console.log shows in the run (up to 50 lines). Up to 20,000 characters.

Needed before the flow can run.

What comes out

Each item that leaves the block carries these fields, to use in the blocks after it as {{field}}.

What your code returns

Each item returned goes on; anything that is not an object becomes { value }.

  • street, district: For example, from const [street, district] = item.address.split(", ").
  • label: From item.label = item.rating >= 4.5 ? "top" : "ok".
  • owner: From item.owner = vars.sales_owner, a variable of the flow.
  • the step says: How many items came out and how many were dropped, and what your code logged: 4 items from your code, 2 dropped, console: rows: 6.

How to use it

B2B sales

Street and district as separate CRM fields

A sales team’s CRM has separate street and district fields; the map gives one address line.

const [street, district] = item.address.split(", "); return { ...item, street, district };

Retail

The 3 best rated in each city

A franchise team wants a short list per city, not one long list for the country.

Before: Sort and limit kept the 9 best in the country, all in Sao Paulo.

With the flow: Three per city, the best rated first, in one email.

FMCG distribution

A route code for each shop

Code is handy when the rule is more than one formula; for one formula, Calculated field is simpler and safer.

Logistics

Merge a webhook’s nested parcels

A courier’s system sends one webhook call per route with the stops nested inside; the flow needs one item per stop.

On the whole list, return items.flatMap(r => r.stops.map(s => ({ ...s, route: r.id })));

Marketing agencies

Tag places by keywords in their name

Banking and payments

Use the flow’s variables

One flow is copied for each regional team; the code reads the region and the owner from the flow’s variables.

Good to know

  • It runs on the server in a sandbox (QuickJS): no web, no files, nothing of the server. At most 3 seconds of CPU, 32 MB of memory, 5,000 items and 2 MB of data in or out, and 20,000 characters of code.
  • What you log with console.log shows in the run’s step. An error stops the step and says which line.
  • Test runs run it too. The editor’s preview without an account passes the items through unchanged.
  • vars holds the flow’s variables (Test with can change them); notes holds run values such as a report written by an xMap LLM block.
  • For a field worked out by one formula, Calculated field is simpler; for sorting and keeping the best, Sort and limit.
  • Custom code is free.

Works well with

  • Calculated field: Adds fields worked out from the others: a number, a text or a yes/no.
  • Filter: Keeps only the items that match a rule.
  • Sort and limit: Puts the items in order, and can keep only the first ones: the 10 best rated, the 20 nearest, the highest fit scores.
  • Webhook / API: Starts the flow when another system sends a POST request with a JSON body to this flow’s webhook URL.
  • Branch: Splits items in two.
  • xMap LLM: Tell the AI what to do in plain words.
  • Remove duplicates: Keeps one of each item when the same one arrives more than once, for example from two searches that overlap.
  • Run another flow: Runs one of your other flows with the items that reach this block, once with all of them or once for each (a loop), and can carry on with what that flow found.

Templates that use it

Stories with it

Industries that use it

Marketing & agencies · Telecom

Try it

Build a flow with Custom code on the home page, no account needed, or book a call. Try it live