I want to add the order number to the right row in my collection to connect it to other data from the website.
My code does not seem to work however. It’s supposed to look for the cartId, which is saved in the collection earlier already, and match it with the one extracted from the order.
Then add the order number in the orderNumber field.
Please have a look:
import wixData from 'wix-data';
// This is the function that Wix Automation will call
export async function invoke(context) {
console.log("Invoke function called. Context:", JSON.stringify(context, null, 2));
// Extract payload data from `context`.
// The exact structure depends on how you've configured your custom integration.
// Usually, the arguments are found in `context.arguments`.
const payload = context.arguments && context.arguments[0];
if (!payload) {
throw new Error("No payload received in invoke function.");
}
// Now call your logic to update the order number
return await updateOrderNumberFromOrderPayload(payload);
}
// Your existing logic to update the order number
async function updateOrderNumberFromOrderPayload(payload) {
console.log("updateOrderNumberFromOrderPayload called with payload:", JSON.stringify(payload, null, 2));
const { orderNumber, cartId } = payload;
if (!orderNumber || !cartId) {
console.error('Missing orderNumber or cartId in payload');
throw new Error('Missing orderNumber or cartId in payload');
}
console.log(`Querying StickerDesigns with cartId: ${cartId}`);
const queryResult = await wixData.query('StickerDesigns')
.eq('cartId', cartId)
.find();
if (queryResult.items.length === 0) {
console.error(`No item found in StickerDesigns with cartId: ${cartId}`);
throw new Error(`No item found in StickerDesigns with cartId: ${cartId}`);
}
const itemToUpdate = queryResult.items[0];
console.log('Found item to update:', JSON.stringify(itemToUpdate, null, 2));
itemToUpdate.orderNumber = orderNumber;
console.log(`Updating item with orderNumber: ${orderNumber}`);
const updateResult = await wixData.update('StickerDesigns', itemToUpdate);
console.log(`Successfully updated orderNumber for cartId: ${cartId}`, JSON.stringify(updateResult, null, 2));
return { success: true, updatedItem: updateResult };
}