Does padStart work in ServiceNow?

Ajay37
Tera Contributor

Hi All,

I have a requirement, to make a string length equal to 7 by adding leading zero's to it.

For suppose if I get 10 --> 0000010, 100 --> 0000100 , 1000 --> 0001000

I have tried the below code in background script and I am getting result as undefined

var id = '1000';

var num = id.padStart(7,"0");

gs.print(num); resulting undefined

Can anyone help?

Thanks

2 REPLIES 2

SumanthDosapati
Mega Sage
Mega Sage

Hi Ajay,

Try this code

var id = 100+"";
    var size = 7;
    while (id.length < size) id = "0" + id;
    gs.print(id);

 

Mark as correct if it works.

Thanks,

Sumanth

Very helpful. Thank you Sumanth..