How to prevent duplicate scheduled API workflows?

I currently schedule a backend workflow when a user clicks a button to save something in my app. The workflow is scheduled to run 1 month later and it sends the user a reminder about the thing they saved.

This works fine if a user saves just one thing. But if they save multiple things (frequently done within the same hour), the workflow gets scheduled to send multiple times and they get multiple emails. Which isn’t great because the emails are already structured to be a summary of their saves (no need to send individual reminders for every individual thing saved).

I’m not sure how to essentially overwrite any duplicate existing scheduled workflows for a particular user if they take this action multiple times. I know scheduled workflows can be deleted, but I’m unclear on how exactly how to locate these scheduled workflows for a user and incorporate conditional logic to prevent running the workflow multiple times in a cluster.

Hi Nico, maybe you can do something like this. Instead of scheduling a workflow a month from now, use a data type named something like ‘scheduledTask’ with a date field executeOn (and other fields needed to perform the workflow with the data it needs). Create a recurring / recursive workflow that runs every day, that checks this data type for new entries (using that field ExecuteOn) and schedules the workflows to be performed using ‘schedule workflow on a list’.

When a user saves a thing, add an entry to this date type ‘scheduledTask’, but before that perform a check to make sure there is not already an entry for that record / action / your use case).

Depending on your use case you can add fields to track the status of a task, the time it took to perform the task etc. etc. Added benefit is that when a workflow error occurs and the status of the task is not what it should be after a workflow is performed successfully, you can add logic that notifies the user (or you) to take the action needed to correct the error.

That makes sense to me, I just thought that was the reason scheduled workflows generated IDs. But scheduled workflows don’t seem searchable, by that ID or anything else. I can definitely see how your approach bridges that gap.

I ended up with a simpler approach I think should work:

  1. Add a new user field named “saved thing reminder workflow ID”
  2. When saving a thing, first check if the current user already has something saved in that new field.
  3. If yes, cancel that workflow ID
  4. If the field is already empty or after successfully canceling the existing scheduled workflow, then schedule the reminder workflow to run 14 days after last seen date.
  5. Save the new scheduled workflow ID to the field.

I based this approach on this tutorial: https://www.youtube.com/watch?v=ZoPQd2z_pBM

1 Like

This topic was automatically closed after 70 days. New replies are no longer allowed.