The CreatorCon Call for Content is officially open! Get started here.

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

Hi Lohitha,



is that mean ID and Vendor ID will have same value?


Do this way,



write a onLoad script,


if(g_form.isNewRecord()){


var num = g_form.getIntValue('<field name>')+1;


incrementNumber = g_form.getValue('number').replace(num,(num+1));


g_form.setValue('<field_name>',incrementNumber );


}


Hi Balaji,



This should be done after the record is submitting.



Thank you,


Lohitha.


vinothkumar
Tera Guru

Hi Lohitha,



If that is the case, what you can do is create a before insert business rule to check the value of last inserted record and split it using Javascript and increment those values in the business rule.


Hi Vinoth,



Thank you for your response, I am new to scripting can you tell me how to start with this.



Regards,


Lohitha.