Code
javascript
// n8n Code node, mode "Run Once for All Items". Name the node "Generate Order ID".
// Expects incoming items shaped like { phone: '...', details: '...' }.
const TIME_ZONE = 'Asia/Ho_Chi_Minh'; // your restaurant's time zone
const PREFIX = 'ORD';
const ALPHABET = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; // no 0/O or 1/I, easy to read aloud
function randomSuffix(length, random) {
let out = '';
for (let i = 0; i < length; i++) {
out += ALPHABET[Math.floor(random() * ALPHABET.length)];
}
return out;
}
function localDay(now, timeZone) {
// The en-CA locale formats dates as YYYY-MM-DD.
return new Intl.DateTimeFormat('en-CA', {
timeZone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
}).format(now);
}
function makeOrderId(now, taken, random) {
const day = localDay(now, TIME_ZONE).replace(/-/g, '');
let id;
do {
id = `${PREFIX}-${day}-${randomSuffix(6, random)}`;
} while (taken.has(id)); // no duplicates inside one run
taken.add(id);
return id;
}
function buildOrders(items, now = new Date(), random = Math.random) {
const taken = new Set();
return items.map((item, index) => ({
json: {
// Keys match the sheet headers, so the Google Sheets node can map them automatically.
'Order ID': makeOrderId(now, taken, random),
'Customer Phone': String(item.json.phone ?? '').trim(),
'Order Details': item.json.details ?? '',
Status: 'New',
Date: localDay(now, TIME_ZONE),
},
pairedItem: { item: index },
}));
}
// Inside n8n, $input exists and the node returns the new rows.
if (typeof $input !== 'undefined') {
return buildOrders($input.all());
}Why it happens
A phone number identifies a customer, not an order. If the sheet is written with "Append or Update Row" matched on the phone column, the second order from the same customer replaces the first. Row numbers are not a safe key either, because they shift when someone sorts or deletes rows. Each order needs its own key that never changes, and the workflow must create that key before writing the row.
Steps
- Add a Code node named "Generate Order ID" after your trigger and paste the code. Adjust
TIME_ZONE, andphoneanddetailsif your incoming fields have other names. - Add a Google Sheets node with the "Append Row" operation. Map the columns automatically. Every order becomes a new row, so the same phone can order again on another day.
- Send the Order ID back to the customer in your confirmation message. They will need it to edit the order.
- In the edit flow, add a Google Sheets node with the "Update Row" operation. Set "Column to match on" to
Order IDand send only the columns that change. - To pass the ID on in the same workflow, use the expression
{{ $('Generate Order ID').item.json['Order ID'] }}in any later node. - To pass it to another workflow, add an "Execute Sub-workflow" node. The sub-workflow starts with "When Executed by Another Workflow". Define an input named
orderIdthere, and fill it with the expression from step 5.
Pitfalls
- The suffix is random, with about one billion combinations per day. That is enough for a restaurant, but it is not a hard guarantee. For strict uniqueness, look up the ID in the sheet before appending.
- Google Sheets may strip a leading
0or read+84...as a formula. Format the Customer Phone column as plain text. - Do not use "Append or Update Row" for new orders. It overwrites the row whenever the match column already has that value.
- The Google Sheets API has per-minute quotas. For busy hours, move orders to a real database and keep the sheet as a report.
Seen on: n8n Community ·
Related service: n8n, Make and Zapier workflow automation