Hey I’m trying to find ow to make a workflow run only a certain number of times. Let say that I want the workflow to run only 1000 times
It looks like your topic is missing some important information. Could you provide the following if applicable.
- n8n version:
- Database (default: SQLite):
- n8n EXECUTIONS_PROCESS setting (default: own, main):
- Running n8n via (Docker, npm, n8n cloud, desktop app):
- Operating system:
@Vasislav, do you mean 1000 loop runs during a single execution or 1000 executions of the workflow (which could be spread over multiple days)?
I suspect the first scenario is what you are after. In that case, you can utilize the built-in variable $runIndex
(see n8n metadata | n8n Docs). For example,
Hi @ihortom ,
Thank you for the fast response. I pretty much want a workflow that runs only 1000 times.
For example, I give the webhook to someone, they use it for 1000 executions and then if they try to use it again its not going to enter the workflow or send them a message that they’ve used too much.
I found this How to create counter - #2 by MutedJam in the n8n forum which seems to be what I need, but I have no clue how to actually do it.
That probably caused by the n8n version difference. You never specified what n8n variation and version you are running. I tested it locally in 1.56.2
Anyway, you seem to be interested in the 2nd option I mentioned.
Indeed, to achieve that you need to utilize Static Data. That implies a bit of coding in Code node, something like below.
Thus, the “Workflow logic” will be executed 1000 times. After that, the “Reject logic” will be engaged instead. You can build your own logic that suits your user case most.
Note I used the schedule node (named “Webhook”) that runs the workflow every minute to demonstrate the usage of the static data.
Beware that the static data won’t be saved between executions unless you:
- Are using an automatic trigger node (such as schedule or webhook)
and - Have activated the workflow
Also, be aware that it gets saved after the execution is finished. So it will only be accessible for executions that started after the previous one is done.
Hey @ihortom,
I really appreciate the effort.
I tried the workflow, but it does not how many times it has been executed. Maybe I`ve done something wrong?
This the the workflow that I want to make execute only 1000 times:
I send a post webhook from ghl to the webhook in n8n and every request sends only 1 contact from ghl to n8n. I will make a video when I get back home.
Thank you in advance!
I didn’t get what problem you have encountered. I don’t expect any. What I expect to see in your workflow the following.
Use my Code node I called “Initiate execution index” together with my IF node called “Initiate execution index” after your Webhook. The Code node keeps track of the executions and the IF node checkes how many times the workflow was triggered via Webhook.
The true path would have the rest of the “authorized” path while the false would have the logic where you would respond back to your customer that they have reached the limit of 1000 executions.
Surely, for the test purpose reduce the number from 1000 to, say, 3 in the IF node. Make sure the workflow has been activated and you use the webhook production URL. Each time the workflow has executed and successfully completed (! it’s an important condition) the Code node output will have executionIndex
incremented.
Hey @ihortom,
Most probably I do something wrong, but the execution index did not change, I’ve made a 1 min video. https://vento.so/view/609483e5-b825-46ee-909a-5fb7453bd880?utm_medium=share
Thank you for the help!
Hi @Vasislav , it is actually my fault. Could you, please, amend the expression in the Code node
workflowStaticData.executionIndex =
!workflowStaticData?.executionIndex ? 1 : // [bug] replace 0 with 1
workflowStaticData.executionIndex + 1;
The problem is, my original expression was always evaluated to true and thus assigning 0
to the static data with each execution rather than incrementing it with a subsequent execution.
PS. I’m modifying my original workflow too to avoid confusion.