How do I compute a field with a Code step?

A Code step runs a short JavaScript function that computes fields (an effective date, a field for couples only) before sending to your webhook, OGGO Data or Nova.

Updated Open this screen
Where it happensIntegrationsAdd a stepCode

What this step does

Some fields your tool expects exist nowhere on our side: they are computed. An effective date on the first day of next month depends on the day of sending. The partner's name must only go out when the prospect lives as a couple. The Transform data node rewrites values with lists; the Code step covers what lists cannot express.

You write a function that receives the lead's fields and returns named fields. Those fields become markers that the webhook, OGGO Data and Nova placed below can use. The function computes, it never sends anything: sending is still done by the destination, with its own settings.

What you need

  • A flow already created, with at least one webhook, OGGO Data or Nova destination below the step.
  • The right to manage integrations on your account, see What can each role do?.
  • Some JavaScript. A ten-line function is enough in most cases, and the example provided covers the two most common needs.

The function's contract

The function receives a data object and returns an object whose keys are field names:

``js (data) => { const now = new Date(); const start = new Date(now.getFullYear(), now.getMonth() + 1, 1); const dd = String(start.getDate()).padStart(2, "0"); const mm = String(start.getMonth() + 1).padStart(2, "0"); return { date_effet1: ${dd}/${mm}/${start.getFullYear()}, ...(data.subscriberType === "couple" ? { nom_conj: data.lastName } : {}), }; } ``

  • What it receives: the same fields the Transform node sees at this point of the flow, including the rules of the steps above. Values arrive as text.
  • What it returns: text, number, true/false or null values. Each one is sent as text, 2,000 characters at most.
  • A missing key, or one set to undefined, means "no field". That is what makes a field conditional: in the example, nom_conj only exists for a couple.
  • Names: letters, digits, _ and -, starting with a letter or _, 64 characters at most. phoneNumber is reserved: a Code step cannot replace the number that Nova and OGGO Data read to reach the prospect.

The limits

The function runs apart from the platform, in an isolated space:

  • no network: no fetch, no call to another service;
  • no require, no module, no access to environment variables or to any secret;
  • at most 100 ms of compute and 32 MB of memory;
  • at most 20 KB of source and 100 fields of output;
  • no async and no promise: with no network, there is nothing to wait for.

new Date() works, and counts days in the platform's time zone.

The path, from the dashboard

1. Open the Integrations screen

In the left-hand menu, click Integrations, then open the flow to set up.

2. Add the "Code" step

Click the canvas + where you want it. Under Logic, choose Code. The step arrives with the example above already written and its two outputs already declared.

3. Write the function

Below the code area, the Available fields list shows this flow's fields. Clicking a field inserts it into the function at the cursor. The Tab key indents; to leave the area with the keyboard, press Escape then Tab.

4. Declare the outputs

Only the fields listed under Declared outputs reach the destinations below. A field the function returns but that is missing from this list is not sent, and the screen says so.

Also declare the fields that only appear sometimes. A trial on a single prospect will never return nom_conj: without a declaration, that field would go out for no couple, and the {{nom_conj}} marker would be refused at activation as an unknown field.

5. Click "Try it"

The function runs on the latest lead received in this flow's scope, or on a fictional example if you have not received anything yet. Nothing is sent. The screen shows the returned fields, what the function received, and the fields produced without being declared, with an Add to outputs button.

6. Use the fields in the destination

In the webhook, OGGO Data or Nova placed below, the declared outputs appear among the fields offered. In a webhook, use them like any other field: {{date_effet1}}.

Conditional fields in a webhook

By default a marker with no value goes out empty: nom_conj= for a single prospect. The Omit parameters whose marker is missing box, in the webhook settings, then removes the parameter instead of sending it empty. It is ticked by default on a webhook you add; a webhook already saved keeps its behaviour until you tick it.

When the function fails

The trial and the history say what happened, with a sentence followed by the function's message:

  • syntax error: the source cannot be read. The message often names the line; line numbers are shown to the left of the code area;
  • more than 100 ms of compute: most often an endless loop;
  • more than 32 MB of memory;
  • result refused: the function did not return an object, or a name, a value or the number of fields exceeds the limits;
  • error while running: the function threw an error, for example by reading a property of a missing value.

An error in the function itself (syntax, refused result, thrown error) is not retried: the same function on the same lead would give the same error. The sending is marked as failed in the history, and the destinations below do not go out for that lead. Exceeding the time or memory limit, or a platform that is momentarily saturated, is treated as a temporary failure instead: the sending stays pending and is retried a limited number of times. The trial says which under the error: "try again in a moment" or "fix the function".

What blocks activation

A flow with a Code step can always be saved. It does not activate if:

  • the step is empty: "The Code step is empty: write the function to run.";
  • the function fails on the trial example: open the step and click Try it to read the error;
  • the Code step is disabled on the platform: the flow stays saved, and activation waits until it is back in service.

Read next

Still missing an answer?

Support can see your account and your deliveries. Asking them is faster than searching any longer.

Contact support