We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Autoincrement string field(custom)

chinni
Tera Expert

Hi All,

I have a string field (vendor) which needs to be incremented with a value(V-001 so on) this should be generated when ever a new record is created.

Please let me know how I can achieve this?

Regards,

Lohitha.

1 ACCEPTED SOLUTION

Hi Lohitha,



Use the below script to extract the number from the vendor field and you have to pass the last created record of your custom table. I have tried the below script to extract the number and it was working and you have to try similar as below.



function get_numbers(input) {


    return input.match(/[0-9]+/g);


}



var m = new GlideRecord('table_name');


m.orderByDesc('sys_created_on');


m.query();


if(m.next())


{


var string = m.vendor;


var first_test = get_numbers('string');


first_test = parseInt(first_test) +1;


gs.print(first_test);



current.vendor = "value of prefix" + first_test;


View solution in original post

14 REPLIES 14

Shishir Srivast
Mega Sage

you can create a Number maintenance on that table. Please refer the OOTB Number maintenance


find_real_file.png


sarfraz3
Mega Expert

Yes, This will help:



Managing Record Numbering - ServiceNow Wiki



thanks


note: please hit helpful/correct depending upon the impact


vinothkumar
Tera Guru

Hi Lohitha,



Navigate to system definition > Number Maintenance and click on new, mention the prefix based on your requirement and select the table.



find_real_file.png


chinni
Tera Expert

Hi All,


vendor


My issue is not with the number maintenance, I have two fields One is ID and other is vendor ID which act as serial numbers. They need to get auto incremented when ever a new record is created. If I use auto number as you have mentioned I get ID as 1 and vendor ID as 2 and when another record is created ID is 2 vendor ID is 4.



My output should be independent of other fields.



Regards,


Lohitha.