Update a previous variable to make a loop n times

Describe the problem/error/question

I would like to to the following worflow:

  • Call a URL.
  • If the result is what I excepted, do something (success).
  • Else, wait 5 seconds and retry.
  • If it failed 3 times, do something (error).

Pseudo code

retry = 3
while retry > 0:
    wait(5 seconds)
    result = request("http://myurl.com")
    if result.text == "OK":
        do_something_on_success()
        break
    retry = retry - 1
if retry == 0:
    do_something_on_error()

To do that, I create a variable retry in a Set, with the value 3.
After my request, if the result is not what I expected, I test the value of retry.
If it’s greater that 3, I update it to retry - 1 and loop on the request statement (with a pause before). If retry is 0, I consider it in “error” and I don’t try anymore.

My problem is that I don’t know how to update this value in another Set.
The variable is accessed by {{ $('Name of the block').item.json.retry }}. I would like to use a variable global to the whole workflow.

Workaround

I found a workaround transforming my loop into recursive calls of my workflow, but I don’t really like the readablility of it.

My workflow

Information on my n8n setup

  • n8n version: 1.45.1
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): ?
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Debian

Hi @PhunkyBob

Welcome to the community!

You can check runindex to see how often it has failed. So you do not have do do anything fancy to see how many retries there were. :slight_smile:
In the second if just check if the runIndex is smaller than what you want it set to and you are done. :slight_smile:

{{ $runIndex }}

1 Like

Hello BramKn,
Thank you for your answer.

In that specific case, I can use the {{ runIndex }} variable.

But my general question is “how can I use (set and update) a variable in a N8N workflow?”.

In a Code block, I tried:

const workflowStaticData = $getWorkflowStaticData('global');
workflowStaticData.my_variable = 'something';
console.log(workflowStaticData.my_variable)
return $input.all();

In another Code block, I succeed to read / update the variable.
But how can I use it in an If statement?

I tried {{ $getWorkflowStaticData('global').my_variable }} without success.

Hi @PhunkyBob

Sorry, didnt see that question.
You cannot use those variables in expressions only in code blocks.

I personally never use the workflow variables, as it only saves when the workflow saves. And it causes the workflow to save every time it runs, which I do not like.
I use Redis to store variables. But you can use what ever you like of course.

If you have a license and get the variable feature you can read variables in expressions, but don’t think you can set them in workflows yet.

As a developer, I find it very odd to not have the ability to update the value of a variable and use it in a condition.
For me, it seems to be a mandatory feature. :laughing:

Maybe it’s because I’m new to N8N and I have to bend to the N8N’s philosophy…

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.