Loop until condition is met

Its possible to do a loopUntil workflow, but it requires a bit of javascript

Create a variable to handle your loop timeout reference (I usually call them timeout with a prefix to precise the logic/functonality of it or put it in a folder)

Create a global workflow:

  • First action must cancel the previous loop (i will come back to that later). We use a customjs action with the following code
    clearTimeout(variables.timeout) (with variables.timeout being the variable you created before). This is not mandatory, but it can avoid to start too many loop in parallel ny mistake

  • Next action being a filter action checking your stop action (like your receive a status ok)

  • Next actions will handle your logic

  • The last action will be a customJS action, with the following code (this will start the loop)
    variables.timeout = setTimeout(() => { executeWorkflow(workflowId) }, 2000)

    • Last parameter (2000) is the interval in milliseconds of your timeout, up to you to pick a good value depending on your usecase. Too much will flood your server, not enough will delay the user experience
    • Workflow id is the id of your workflow, you can have it display by activating the dev information (on the dev panel

You can have a variation of this with a setInterval logic.

We know this is not ideal, but this kind of logic can lead to important bugs (infinite loop, server flooding), so we really want to think about it before making it to easy to push by mistake. But we are aware of this usecase, and working on it :slight_smile:

Also please be sure that if you go this way, you must be very vigilant that one day your condition will be met, or you will have infinite loop. By security you can add a maximum number of tries or times on the condition.

2 Likes