Is it advisable to use Wix Automation Triggers within the afterInsert and afterUpdate data hooks?

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.

And I guess to add to my original post here, I’m just concerned about timeouts. I’m assuming my code and automations aren’t doing too much to worry about a timeout occurring. But I’m wondering if it would be less likely to timeout if I chose triggered emails over the automation triggers. Or instead of worrying about having this logic within the data hooks and wix cms dashboard, if it would be best to create a dashboard page using the wix cli that is connected to one of my cms collection and just add the logic within a form submission to handle all the updates and email automations. As the emails will only trigger when an admin updates the item for the cms collection.

I don’t see a reason why you couldn’t use the trigger code within the data hooks - what I’d probably recommend is understanding when you actually want to fire the trigger.

For example, if updating Boolean value shouldn’t trigger the automation (but can be updated by the automation), then add a check within the hook so it only runs when you specify.

IIRC, it’s possible to compare the new data to the old, so you can check if the Boolean value change is the one to cause the hook to run and then stop the Automation trigger code from running

Ok nice, thanks for clarifying that it’s fine to use automation triggers within the data hooks. And now that you say it, I’m not sure why it didn’t occur to me that doing a check for the boolean value was possible to stop the Automation trigger from running. Maybe because I’d have to do quite a long logical check to account for all the various status emails that I have. But thanks for the help, really took some worry off my mind :grin: