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

In that case you can use onSubmit client script and do a GlideAjax on your table to get the previous value from table and then set it with an increment on your form.


please have below sample code, you need to make some change to make it work. I haven't tested the code but you can get an idea how it will work.



Script include:


var DisplayDetails = Class.create();


DisplayDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getDetails : function() {



  var newRecord = new GlideRecord('your table name');


  var sysid = this.getParameter('sysparm_sys_id');


  newRecord.addQuery('sys_id', sysid);


  newRecord.query();



  if(newRecord .next())


  {


  return newRecord.<field value name>


  }


  },


  type: 'DisplayUserDetails'


});




Client Script:


function onSubmit() {


  if (isLoading || newValue === '') {


  return;


  }



  var output = "";


  var sys_id = g_form.getUniqueValue();



  var userDetails = new GlideAjax("DisplayDetails ");


  userDetails.addParam("sysparm_name", "getDetails ");


  userDetails.addParam("sysparm_sys_id", sys_id);


  userDetails.getXML(ajaxResponse);



  function ajaxResponse(serverResponse) {


  var answer = serverResponse.responseXML.documentElement.getAttribute("answer");


        output = answer.split('-');


        var num = parseInt(output[1])+1;


        var increment = output[0] + '-' + num;        


        g_form.setValue('<field name>',increment);


  }


  }


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;


Hi Vinoth,



It's giving this error



find_real_file.png



Thank You,


Lohitha.


Hi Lohitha,



I have tried in my background script by supplying some character and number values to the string field and it worked fine.



Have you tried parseInt in your script, I am not sure why it was not working.