Boundary Script Counter

I’m trying to work out a complex boundary script, but I’m starting simple, and that is just to emulate a record count of two. Every two CSV records becomes a “data page”, or in other words, set a boundary every 2 records. What the script below achieves is 1 CSV record in the first Data Model record, and every subsequent is correctly at 2 CSV records.

What’s wrong with my loop logic that causes it to fail on the first iteration?

var i;

if (boundaries.getVariable("counter") != null) 
{
  i = boundaries.getVariable("counter");
}
else
{
  i = 1;
}

if (i == 2)
{ 
  boundaries.set();
  i = 1;
}
else
{
  i = i + 1;
}

boundaries.setVariable("counter",i);

I believe there’s a delimiter “before” the first line (or put another way, the delimiter occurs before the next content). The following script would work:

var counter = boundaries.getVariable("counter") || 0;
if (counter % 2 == 0) { boundaries.set(); }
boundaries.setVariable("counter",++counter);

Thanks, Phil. My first attempt was just to get the counter logic down. I needed the larger framework with nested IF statements to work it out fully. But based on your insight, I modified my script to check for a “non 0” value, and got it working.