Code
javascript
// n8n Code node, mode "Run Once for All Items".
// Place it before the Google Sheets "Append Row" node.
const TIME_ZONE = 'Asia/Ho_Chi_Minh'; // the restaurant's time zone
const ALPHABET = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; // no 0, O, 1, I
function dayStamp(date, timeZone) {
const parts = new Intl.DateTimeFormat('en-US', {
timeZone, year: 'numeric', month: '2-digit', day: '2-digit',
}).formatToParts(date);
const p = Object.fromEntries(parts.map((x) => [x.type, x.value]));
return `${p.year}-${p.month}-${p.day}`;
}
function makeOrderId(date = new Date(), random = Math.random) {
let suffix = '';
for (let i = 0; i < 6; i++) {
suffix += ALPHABET[Math.floor(random() * ALPHABET.length)];
}
return `ORD-${dayStamp(date, TIME_ZONE).replace(/-/g, '')}-${suffix}`;
}
function buildOrderRow(order, date = new Date(), random = Math.random) {
const details = order.details ?? '';
return {
'Order ID': makeOrderId(date, random),
'Customer Phone': String(order.phone ?? '').trim(),
'Order Details': typeof details === 'string' ? details : JSON.stringify(details),
'Status': 'New',
'Date': dayStamp(date, TIME_ZONE),
};
}
// Inside n8n, $input exists and the node returns here.
if (typeof $input !== 'undefined') {
return $input.all().map((item) => ({ json: buildOrderRow(item.json) }));
}Why it happens
A phone number identifies a customer, not an order. If the sheet node uses "Append or Update Row" matched on the phone column, each new order replaces that customer's previous one. Google Sheets has no built-in primary key either, so the workflow must create one. Once every row has its own ID, editing and passing the order around both become simple lookups.
Steps
- Add a Code node named
Generate Order IDafter your trigger and paste the code. It expectsphoneanddetailson each incoming item. - The node returns one item per order, with keys that match the sheet columns: Order ID, Customer Phone, Order Details, Status, Date.
- Add a Google Sheets node with the operation "Append Row" and map the columns automatically. Do not use "Append or Update Row" here.
- To pass the ID on, add an "Execute Sub-workflow" node. Set an input named
orderIdto{{ $('Generate Order ID').item.json['Order ID'] }}. - In the second workflow, start with the "When Executed by Another Workflow" trigger and define the
orderIdinput there. - To edit an order, use a Google Sheets node with "Update Row". Set "Column to match on" to
Order IDand send only the changed fields.
Pitfalls
- The suffix is random: six characters from 32, about one billion combinations per day. A collision is unlikely but possible. For a hard guarantee, look up the ID in the sheet before appending and regenerate on a match.
- Format the Customer Phone column as plain text. Otherwise Google Sheets drops leading zeros and the plus sign.
- Node names differ between n8n versions. Older versions call the nodes "Execute Workflow" and "Execute Workflow Trigger".
- "Update Row" changes only rows that match. If the ID is not found, nothing is updated, so check the node output and handle that case.
- Keep Google credentials in n8n Credentials, never inside the Code node.
Seen on: n8n Community ·
Related service: n8n, Make and Zapier workflow automation