Help with auto increment a value in a catalog item variable

SD29
Tera Expert

Hello All, 

I am trying to populate 2 variables by concatenating & incrementing 4 different variables using onChange client script based on a Quantity variable. 

Variables: A,B,C,D,E,F

A,B,C are reference fields

D is a single line text with default value as '00'. 

If the Quantity is 1 then the variable E will be populated as ABC00

If the Quantity is 2 then the variable F should be populated as ABC01, but the script i'm using is populating the variable as ABC1 instead of ABC01. 

please let me know where i'm doing mistake.

Script: 

var qt = g_form.getValue('a');
var en = g_form.getValue('b');
var eco = g_form.getValue('c');
var it = g_form.getValue('d');
var hn = en + eco + it;
if(qt == 1){
g_form.setValue('e', hn);
}
var it2 = ++it;
var hn2 = en + eco + it2;
if(qt == 2){
g_form.setValue('d', hn);
g_form.setValue('e', hn2);
}

Thanks,

SD

1 ACCEPTED SOLUTION

If you need to use numbers and store them in a field with padded zeroes, you can write a function like this:

function padWithZeroes(number, len) {
var output = number.toString();
while (output.length < len) {
output = "0" + output;
}
return output;
}

var test = padWithZeroes(13, 5); // test equals "00013"

View solution in original post

5 REPLIES 5

Rogers Cadenhe1
Giga Guru

This statement is making it2 a number instead of a string:

var it2 = ++it;

That's why the preceding zero in it is disappearing.

Instead of using the increment operator it looks like you should do this:

var it2 = "01";

Hello Rogers, 

Thanks for replying!

if i use it2 = 01, then i think if the D value 20, the E value will be populated as abc20 & the F value will be populated as abc01 which is not correct. 

If the D value is 00 then i want the result to be abc00 & abc01

If the D value is 10 then i want the result to be abc10 & abc11. 

I think your suggestion will result as abc01 irrespective of D value.

Thanks,

SD

If you need to use numbers and store them in a field with padded zeroes, you can write a function like this:

function padWithZeroes(number, len) {
var output = number.toString();
while (output.length < len) {
output = "0" + output;
}
return output;
}

var test = padWithZeroes(13, 5); // test equals "00013"

Hi Roger,

My output value will not always be limited to 5 characters and it's not all numbers. 

Let's Say: A = pI, B = FRAU, C = TAF, D = 00

Here is an example of output i'm expecting: 

1) pIFRAUTAFC00

2) pIFRAUTAFC01

3) pIFRAUTAFC02

 

Output i'm getting is 

1) pIFRAUTAFC00

2) pIFRAUTAFC1

3) pIFRAUTAFC2

 

Thanks,

Sd