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.
Hey just tried it and its working!!!
Do you have an idea why I might get this error when I try to call the other workflow?
It sounds like what Vasislav actually wants isn’t a loop at all, it’s a usage limit on the workflow for a particular client, identified by webhook address.
In that case, it seems to me what you actually have to do is increment a counter in something external (like AirTable), and refuse to run if it increments over 1000. Each Airtable row would represent one client, and have webhook_address and usage_increment as columns.
Update: It works, but when I add let say 100 contact in the workflow, the counter skips a lot. Meaning that it counts 100 contacts (which should be 100 executions of the workflow) as 5,10 or 15.
Hi @JayF, thanks for your suggestion. I think that you are right. The only thing that I can include is that I send 100-2000+ contacts individually to the webhook.
So, I’m not sure if that is going to mess something up? Can you refer me to a tutorial so I can test your suggestion?
Thanks in advance!
If I’m correct in assuming what you want, it would look like this:
You just have to get a free AirTable account and fill in your credentials, then use the getter and setter here to retrieve/set the increments for any client. In the getter node you can set a condition like ‘client_id==webhook_address’ to get the correct row, and from that send the increments
column into the If
node.
Now, if you want to use the same webhook for multiple clients and have each one have their own usage limit, that’s a slightly more complex setup and you’d need some kind of client authentication. You can look for examples for that in Discover 800+ Automation Workflows from the n8n's Community
It looks like an issue with the (sub-)workflow you are trying to run with “Execute Workflow”. That sub-workflow would return the data from the last node executed. Therefore, check the execution of the workflow with ID “AjwhfaCqDqaaqmea” and see what fails there. As the actual error indicates the function replace()
is called on non-existing (null
) item.
I`m trying it right now but it will take me some time to test it. Do you think that it will work if there are 10+ requests per second.
Hi @ihortom,
The workflow AjwhfaCqDqaaqmea, works fine by itself.
Even when I add the execute workflow:
but when I try to add an execute workflow at the end of the other workflow it does not work for some reason and gives the error that I mentioned previously
Your screenshots dd not convince me. From them I conclude the last nodes do not produce any items.
Not sure what happened with the screenshot tbh. https://vento.so/view/5ef61153-bc66-4576-bf83-6f63d6549b58?utm_medium=share