JavaScript examples: IDs, codes and tokens

Code for the Run Custom Code action that makes UUIDs, short codes, one-time codes and reference numbers, plus how Tadabase record IDs look.

  • Verified Nov 17, 2026
  • Classic builder

On this page
  1. Random values in the code
  2. How record IDs look
  3. Generate a UUID
  4. Short order code, such as ORD-AB12CD
  5. Six-digit verification code
  6. Readable token without look-alike characters
  7. Promo code with a fixed prefix
  8. Readable code built from record values
  9. Reference number with the date in it
  10. Idempotency key for an outgoing API call
  11. Short hash for spotting duplicates
  12. Correlation ID for tracing a run across systems
  13. Several codes at once
  14. A random token is not a signature

These examples go in the code of a Run Custom Code workflow action. Each one makes an ID or code and passes it on with returnData, so a later step can save it to a field or put it in an email. New to the action? Start with Running Custom Javascript.

Random values in the code #

For anything that must be hard to guess, such as a verification code or a link token, use crypto.randomUUID() or crypto.getRandomValues(). Do not use Math.random() for those: its output can be predicted.

The examples read inputs as $name. An input exists only if you added it under Data to use in the code with that Variable name; using a $name you did not add stops the code with an error.

How record IDs look #

Your code never sees a record's internal number. Record IDs reach the code as short encoded text, for example DVWQWRNZ49:

  • $record.id, the ID of the source record.
  • Any id, record_id or other key ending in _id inside a record object.
  • Connection fields, which arrive as a list of encoded IDs (an empty list when nothing is connected).

Treat an encoded ID as plain text: compare it as a string and pass it on unchanged. A value you return this way can go straight into a Connection field in a later step. An ID identifies a record; it is not a password, so do not use it alone to protect a link.

Generate a UUID #

JavaScript
returnData("id", crypto.randomUUID());
// e.g. "3b241101-e2bb-4255-8caf-4136c566a962"

Short order code, such as ORD-AB12CD #

JavaScript
const bytes = new Uint8Array(3);
crypto.getRandomValues(bytes);

const hex = Array.from(bytes)
  .map(function (b) { return b.toString(16).padStart(2, "0"); })
  .join("")
  .toUpperCase();

returnData("order_code", "ORD-" + hex);

Three random bytes give about 16 million combinations. That is fine for a readable reference, but two records can still get the same code, so do not rely on it being unique. Use a UUID, or the record ID, when it must be unique.

Six-digit verification code #

JavaScript
const buf = new Uint32Array(1);
crypto.getRandomValues(buf);

// 000000 to 999999, leading zeros kept
const code = String(buf[0] % 1000000).padStart(6, "0");

returnData("verification_code", code);

Readable token without look-alike characters #

JavaScript
// No 0, O, 1, I or L, so people can type it from a printout.
const ALPHABET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789"; // 31 characters
const LENGTH = 8;

let token = "";
while (token.length < LENGTH) {
  const bytes = new Uint8Array(LENGTH);
  crypto.getRandomValues(bytes);
  for (const b of bytes) {
    // 248 = 31 x 8. Skipping higher bytes keeps every character equally likely.
    if (b < 248 && token.length < LENGTH) {
      token += ALPHABET[b % ALPHABET.length];
    }
  }
}

returnData("token", token);

Promo code with a fixed prefix #

JavaScript
// Input: prefix (Custom Value, e.g. WELCOME)
const ALPHABET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789";
const prefix = String($prefix || "PROMO").toUpperCase();

const bytes = new Uint8Array(5);
crypto.getRandomValues(bytes);
let suffix = "";
for (const b of bytes) suffix += ALPHABET[b % ALPHABET.length];

returnData("coupon", prefix + "-" + suffix); // e.g. WELCOME-7K9P2

Readable code built from record values #

JavaScript
// Inputs: customerName and invoiceNumber (Record Value (Source))
// Result: e.g. "acme-corp-INV-001234". The same inputs always give the same code.
const customer = String($customerName || "unknown")
  .toLowerCase()
  .replace(/[^a-z0-9]+/g, "-")
  .replace(/^-+|-+$/g, "");
const number = String($invoiceNumber || "0").padStart(6, "0");

returnData("invoice_code", customer + "-INV-" + number);

Reference number with the date in it #

JavaScript
// e.g. "REF-20261117-9F1A2B"
const datePart = new Date().toISOString().slice(0, 10).replace(/-/g, ""); // date in UTC

const bytes = new Uint8Array(3);
crypto.getRandomValues(bytes);
const suffix = Array.from(bytes)
  .map(function (b) { return b.toString(16).padStart(2, "0"); })
  .join("")
  .toUpperCase();

returnData("reference", "REF-" + datePart + "-" + suffix);

toISOString() gives the date in UTC, so a run late in the evening in the Americas can show the next day's date. getAppDetails().timezone tells you the app's time zone if you need to adjust for it.

Idempotency key for an outgoing API call #

Some APIs accept an idempotency key so that a repeated request is not processed twice. The key must be the same every time you retry the same operation, so build it from values that do not change instead of from random bytes:

JavaScript
// One key per record per day: a second run today sends the same key.
const day = new Date().toISOString().slice(0, 10);
const recordId = $record ? $record.id : "no-record";

returnData("idempotency_key", "invoice-" + recordId + "-" + day);

Short hash for spotting duplicates #

JavaScript
// Same text in, same hash out. Good for grouping and spotting duplicates.
// NOT for passwords or anything secret: it is easy to reverse or collide.
// Inputs: email and createdAt (Record Value (Source))
function hash32(s) {
  let h = 0;
  for (let i = 0; i < s.length; i++) {
    h = ((h << 5) - h + s.charCodeAt(i)) | 0;
  }
  return Math.abs(h);
}

const key = String($email || "").toLowerCase() + "|" + String($createdAt || "");
returnData("dedupe_key", hash32(key).toString(36));

Correlation ID for tracing a run across systems #

JavaScript
// App ID + record ID + a random part. Send it to other systems in a header
// or field so you can find the same run in every log.
const correlationId = [
  getAppDetails().id,
  $record ? $record.id : "no-record",
  crypto.randomUUID().slice(0, 8)
].join(":");

console.log("Correlation ID:", correlationId);
returnData("correlation_id", correlationId);

console.log lines show on the Console tab when you click Run in the code window, and in the step's run log.

Several codes at once #

JavaScript
// e.g. 25 invite codes in one list
const COUNT = 25;
const codes = [];
for (let i = 0; i < COUNT; i++) codes.push(crypto.randomUUID());

returnData("codes", codes);
returnData("count", codes.length);

A random token is not a signature #

A random token in a link makes the link hard to guess, but it does not prove who made the link or that nothing in it was changed, and it does not expire by itself. If your process needs that:

  • Store the token, with an expiry date, on the record, and check both before you act on the link.
  • Keep any signing key or shared secret in an app secret and read it with getAppSecret("NAME"). Never type a key into the code: the code is saved with the workflow and other builders can read it.
  • For real signed tokens, let the system that checks the token create it.

Was this page helpful?

Something wrong or missing on this page? Tell us.