Creating a Counter Variable for a Loop

Hi,

So - here is the basic workflow.

I have files in a google drive directly, which the flow reads in, and then a loop will process through each file name one at a time, and add some information to a google sheet. However, after a specific number of files, a date needs to be changed in the google sheet column, and incremented by one day.

By example - the file list is traversed, a filename and file details are added to a row in the google sheet, and a date is added to that row. After the loop runs 3 times, then the date must be incremented by one day, and the process continues.

My problem is, that when using the “put” from persistent storage, the data is set to 3, and I decrement it by one after each loop, but for some reason, when testing that with the “if/then” it seems to never actually do anything.

basically I’m trying to create a “if x>0 then do…else” kind of situation, where x is reduced each run of the loop.

Is there some way to do this with Activepieces, or do I have to use the code part to achieve this?

Hi there @planetx, I would personally do it this way, let us know if it helps you:

https://cloud.activepieces.com/templates/AQ8G7O8m0iJkRpHtt2CiS

Hi Abdul,

That’s great - I am actually attempting to do precisely as you have done, but for some reason, it doesn’t seem to be working. I’m looking further into it, and will update shortly if I am able to determine what is causing it to ignore my stored counter value after I do the subtraction operation and storage.

HI Abdul,

Somewhat frustrated by all this. As the counter never seems to change.

Couple questions:

  1. I’ve set the actual object to the scope of “Flow”, as I don’t want the variable to be stored on a project scope as it will change each time the flow runs based on data provided by the trigger.

  2. Are there any examples of how this works, or tutorials I can review - as I feel so absolutely clear that I’ve done everything correctly, yet the counter simply never changes it’s value, even after doing the ‘math’ operation on it, and then storing it again using ‘put’ …

I’m quite frustrated, so any guidance you can give is appreciated.

Hello buddy, sorry for your frustration, can you let me know how you tested the counter and how it didn’t work ?
like did you test the step multiple times ? or did you test the flow multiple times and it is not working after being published and really triggered, also can you please share your flow with me ? just click on the chevron next to the flows name in the builder or the three dots next in the flows’ table to share it.
You could export it if you are self hosting.

Hi Abdul,

Thanks so much - I’ve messaged you with the flow.

Hopefully there is an easy solution, and I can post the solution here to ensure anyone else running into this issue is able to benefit from any resolution we come up with.

I was playing with counter and storage couple of days ago and worked for me. Here is my test flow that increments counter while going through the loop:
https://cloud.activepieces.com/flows/PnncmV4IbEIMzy9rlHDw5

Hey,

Thanks so much for taking the time to provide that. You are correct, and as it turns out, it all comes down to the “scope” and the “Get” operation with persistent storage prior to making reference to in it in the following piece (at least, this is what I determined after working through the problem).

So - for anyone else who does have the issue I was facing, it’s worth keeping a close eye on the scope of the persistence (flow vs. project), and also using the Get operation of the Persistent Storage piece prior to making additions/subtractions or other operations on it.

I was of the misguided impression that once I had initially placed the key/value pair into persistent storage using the “put” action, that it could easily be referenced in any other piece by just using the key of the object. While that is logical, it appears it is still important to retrieve the object with “get” prior to other operations on it when working within a branch or loop.

Hope that makes sense.

Thanks so much @Flickerko for your flow - this outlines everything brilliantly!

1 Like

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