I’m trying to send automated emails using Wix Automations with the use of the afterInsert and afterUpdate data hooks. However, I’m not sure if this is an advisable approach or whether I should just be using triggeredEmails within the data hooks. I would like to stick with the convenience of Wix Automations as I’m not only sending emails to members, but also to various Wix collaborator roles.
These emails are triggered when I update the status field within the Wix CMS Dashboard for a particular item. But as soon as the emails are sent, I’m also doing a Patch request within Wix Automations to update a boolean field that states whether the email was sent as to not resend the same email when other updates are done within the Wix CMS Dashboard. However, I know this retriggers the whole hook lifecycle, which I feel is unnecessary for my use case. So I’m wonder if there is a better option that I can use to accomplish the exact same things.
This image shows a bit of my Wix Automation logic:
And this is the code I have within an afterUpdate hook:
export async function ShippingOrders_afterUpdate(item, context) {
try {
const member = await getMember(item._owner);
const triggerMethod = auth.elevate(customTrigger.runTrigger);
// Trigger status update automations
await triggerMethod({
triggerId: WixTriggerAutomations.SHIPPING_ORDER_STATUS_UPDATE,
payload: {
shippingOrder: {
cmsItemId: item._id,
orderId: item.title,
status: item.status,
itemWeight: item.verifiedWeight,
amountDue: item.amountDue,
dateReceived: item.received,
sentReceivedEmail: item.sentReceivedEmail,
sentShippedEmail: item.sentShippedEmail,
sentDeliveredEmail: item.sentDeliveredEmail,
sentCanceledEmail: item.sentCanceledEmail
},
member: {
accountNumber: item.accountNumber,
firstName: member.contact.firstName,
lastName: member.contact.lastName,
email: member.loginEmail
}
}
});
return item;
} catch (error) {
console.error(`ShippingOrders_afterUpdate failed for ${item._id}: `, error);
}
}
Any help would be very appreciated as I’m having quite the dilemma with this.
