- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2017 01:56 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2017 04:10 AM
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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2017 03:38 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2017 03:50 AM
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);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2017 04:10 AM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2017 10:09 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2017 06:55 AM
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.