I can't figure out how to schedule a workflow from within an app. The simplest case of what I'm trying to do is to enable users of an app to change the time when a workflow should run. So eg a user of an app should be able to set whether a particular workflow that sends out an email is sent daily, weekly or monthly.
Is this possible at all? If this is not possible, what would possible workarounds look like?
I don't believe you can alter the schedule directly in a workflow. A workaround would be simple enough though.
Schedule your workflow to your highest frequency. In your example above it would be daily. Add a column in your table eg email_frequency and populate this with either an integer for days or text (daily, weekly or monthly).
In one of your first blocks in your workflow, you can filter out which emails should actually be sent based on the stored email_frequency value and the current date.
Here’s an example approach:
Database Setup: Ensure your table has a column like email_frequency and last_sent_at.
Workflow Logic:
Set the workflow to run daily.
Add a JavaScript block that filters users based on their preferred schedule.
Here’s a JavaScript block example to determine if an email should be sent:
const users = getUsersQuery.data; // Assume this retrieves users from your DB
const filteredUsers = users.filter(user => {
if (!user.last_sent_at) return true; // Send if never sent before
const lastSent = new Date(user.last_sent_at);
const diffInDays = Math.floor((today - lastSent) / (1000 * 60 * 60 * 24));
switch (user.email_frequency) {
case 'daily':
return true;
case 'weekly':
return diffInDays >= 7;
case 'monthly':
return diffInDays >= 30;
default:
return false;
}
});
// Return the filtered list for the next workflow step
return filteredUsers;
Send Emails & Update last_sent_at:
Use a loop to send emails to filteredUsers.
After sending, update the last_sent_at column in the database.
Although it does make it seem like workflows and apps are not really tightly integrated with each other which is a bit disappointing, what do you think?
In any case this solves my problem thank you very much
I can understand the frustration and agree workflows should be able to be controlled a little more in app. There is often a workaround to get them to play better together. The Retool team is good at helping out and pushing new features consistently.
Very interesting use case. Apps are able to send data to workflow endpoints via queries to the endpoint trigger URL so that workflows can process data from an app very well.
But I assume that the workflows team may have assumed that the scheduling of workflows would be something that app editors would manage from the GUI of a workflow editor.
But your use case makes sense and I can definitely make a feature request so that workflows can have this API exposed to control workflow trigger options via a REST API!