Pad a field with leading zeros

I am new to planet press connect and making strides. Self learning. I have a sequence number that I need to pad with zeros, but have no idea how. I need every sequence number to be six digits.

examples:

000001; 000010; 000100; 001000; 010000; 100000.

Here is the source code. I tried using the prefix but it makes my 10 sequence numbers seven digits.

add(record.fields[“ID”], function (value) { result += value; });

Just got it figured. Here is full code. Please share if there is a better way.

function padDigits(number, digits) {
return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
}

{
function add(value, operation) {
if (value === undefined || value === “”)
return;
operation(value);
}

var result = "";

// padDigits(9, 4);
add(record.fields[“ID”], function (value) { result += value; });
results.html(padDigits(result, 6));
}

Test this code:

{
var result = “”;
var val = record.fields[“ID”];

var s = “000000” + val;
result = s.substr(s.length-6);

results.html(result);
}

The padding zero’s functionality should be available in one of our next product releases (no ETA yet). However here are a couple of functions that I recommend and which I have tested in Connect that could be of help to you.

Please look at the JavaScript code below.

So you can use this two solutions

Note: you may need to change the field id (record name) so that it works for you.

  • You can limit the maximum number of ‘0’ that you will have on your field.

function padDigits(number, digits) {

__var__ num = "00000" + number;

__return__ num.substr(num.length-digits);

}

{

function add(value, operation) {

     __if__ (value === __undefined__ || value === "")

           __return__;

     operation(value);

}

var result = “”;

add(record.fields[“id”], function (value) { result += value; });

results.html(padDigits(result, 6));

}

  • Or you can also use this other function if you want.

function padDigits(number, digits) {

__var__ num = number+"";

__while__ (num.length < digits) num = "0" + num;

__return__ num;

}

{

function add(value, operation) {

     __if__ (value === __undefined__ || value === "")

           __return__;

     operation(value);

}

var result = “”;

add(record.fields[“id”], function (value) { result += value; });

results.html(padDigits(result, 6));

}

Please let me know if this works for you

Best Regards,

Juan

Thank you I will test all the above code. On a side note, in PLanet Press 7 we were able to create a global variable and increment it by one for each data set.

We used that as a sequence number if one was not present. Is that still available? If so how? And where would I enter that code/variable?

This one worked. :slight_smile: Now to make a decision as there are multiple ways.