I wrote a counter script with the opportunity to set start value, end value and step size.
start value = first number where to start the counter
end value = last number of the counter (if there are records left the counter repeat counting at the start value)
step size = stepsize to the next number
Because of the missing function to read out the total number of records Im looping through all records (static value of total records in my script: 99999) and put the counter values per record in an array. That worked fine for me. Since Connect Designer version 1.4.1 there is a limitation of iterations (max. 264) so my script dont work properly anymore…
How can I create a counter that provides the possibility to set an end value like described?
A standard counter script without the possibility to set an end value is no problem…
PS: I´m using PrintShop Mail Connect, so there`s no Datamapper and no Workflow!
I’m not sure why you wrote a counter script, since there’s already a Generate Counter Wizard in Connect (which is also available for PrintShop Mail)?
If it’s because you already have your existing data and you need the counter on top of that data, then your script does not need to loop through all the records - and that’s extremely inefficient since the script looping through all the records will loop… on each of those records!
One way to manually generate a counter in this case would be to base yourself on the record.index value available in Designer. It’s equal to the current record (1 to however many records you have) so you can use this as a base for your calculation but only for the current record.
For example, a counter that starts at 500 and increments by 10:
val counter = (500 + (record.index * 10)) - 10;
(500 base counter value, plus the record index times 10… the last -10 is so it starts at 500, not 510, since record.index is 1-based and not 0-based)
Essentially, instead of creating your own loop you just use the one that’s already established in the data model!
If that’s not what you need you’ll have to give a bit more information on what kind of couter you’re trying to establish.
I want to use a database and need a dynamic counter too.
For example, a counter starts at 2, increments by 2 and count to 10, than it should start again at 2 untill the end of total records (total records in my database: 12).
record
counter
1
2
2
4
3
6
4
8
5
10
6
2
7
4
8
6
9
8
10
10
11
2
12
4
I don't know how to do that without a loop through all records because I can't get the total amount of records.
You really don’t need to know the number of records to be able to have a counter, even one that resets. Keep in mind that the “loop” is already made for you in the sense that record.index is the index of a loop for each record in your data source. So, really, all you need is some math to get the desired result at this point (see, they told you math would be useful one day!)
But I’m certainly not here to let you bang your head on the desk trying to figure this out. So I did if for you, and the result is a brand new How-To that will benefit everyone! Thanks for the challenge, and here is the prize: