Kenny Wimberly
Tera Guru

TL;DR

I needed a counter for flow designer, so I created one.

If you insist on reading the long version...

My job has not usually taken me down the road of using the flow designer much since its inception. However, in recent months, I've spent quite a bit of time with it. Having been on the platform for 12 years now, I've relied and appreciated mainly on a high/pro-code approach rather than a GUI. That said, I'm currently working on a project where we do not have administrative rights and are required to only develop in an application scope to build a solution. While this is probably not an issue for some of us, I found it quite difficult to adapt. I'm not going to go into all of my challenges here, but I did come across something that seems quite important and necessary that Flow Designer does not have: a counter/turnstile feature. I scoured the community and several other sites looking for a solution and found several that pretty much all said the same thing: create a table that serves as a placeholder for your counter value or even a scratchpad. These are fine solutions, albeit very involved and with some hefty overhead. Being the "lazy" developer that I am (always looking for an easier way to do things), I came up with a bit of a different solution. After talking this issue over with one of my colleagues @John Gubatayao, he mentioned "why not use a for each?". I'd tried that several different ways: using an input that I attempted to modify, tried setting a value on the trigger record (tacky solution), creating an empty variable that I could set during a loop, etc. etc. But, after he mentioned that one thing, I had an epiphany overnight about how to make the for each work. It's quite simple, really. But, I'm one to overthink everything. I'm not claiming this is the first solution or best solution. What I'm sharing with you is my experience and how I resolved my problem.

 

I'll set this up so you can see what I did and replicate it:

First, you need to use a subflow so you can have an input. I created an integer input for my subflow called max_iterations.

find_real_file.png

I then created an action that I called Create Counter Array

find_real_file.png

For the Input on the action, I created a Max Number (max_number) as an integer.

I then created a script step that contains the following:

find_real_file.png

Note the input and output variables. I'll put the code here so that it's easier to copy/paste:

(function execute(inputs, outputs) {
	var countArr = [];
  	//create array given max number
  	var maxNum = parseInt(inputs.max_number);
  	for (var i = 1; i <= maxNum; i++) {
      countArr.push(i);
    }
  
  outputs.counter_array = countArr;
})(inputs, outputs);

Finally, I created an output for the action as follows:

find_real_file.png

And set the value as follows:

find_real_file.png

I then went back to the subflow I created and added an action step for the Create Counter Array at the root of my subflow as follows:

find_real_file.png

From there, I was able to loop through the array and do what I needed to do just like we would with a counter or turnstile.

Keep in mind you'll need an end in your loop if you've accomplished what you needed to during the loop and do not need further loops.

 

As mentioned, I don't spend a lot of time in Flow Designer. If any of you have a better solution. Please let us know. I'm just posting what worked for me because it literally took me two days to get to a solution I was comfortable with. It would be nice if we had an OOB solution, heck, we might and I just don't know it.

Thanks for listening and I hope I help at least one person.

Comments
JohnG3
Mega Guru

Thanks for sharing this helpful solution! 🙂

kevclark
Tera Contributor

This is brilliant, and will totally hold me overuntil we get Flow variables in Quebec.  Well done @Kenny Wimberly !  Give the man some helpful votes!

Version history
Last update:
‎12-08-2020 08:30 AM
Updated by: