Auto Numbering Using Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2018 10:21 PM
Need to Populate "Correlation Id" field on Custom table with numbers starting from 4000 in increments of 1. Tried this Script from Community and Correlation ID is being Populated as Null or NaN. Need help.
Clent Script :
function onLoad() {
//Type appropriate comment here, and begin script below
var prefix = '';
var ga = new GlideAjax("AutoNumbering");
ga.addParam("sysparm_name", "getNum");
ga.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
var final_new_num = prefix + answer;
if(g_form.getValue('correlation_id') == "")
{
g_form.setValue('correlation_id', final_new_num);
}
}
}
SCRIPT INCLUDE :
var AutoNumbering = Class.create();
AutoNumbering.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getNum : function() {
var count = '1';
var gr = new GlideRecord('CUSTOM TABLE');
gr.orderByDesc('correlation_id');
gr.setLimit(1000);
gr.query();
if(gr.next())
{
if(gr.correlation_id != "")
{
var num = gr.correlation_id;
var res = num.substring(3);
var newnum = parseInt(count) + parseInt(res);
return newnum.toString();
}
else
{
var incnewnum = '0';
return incnewnum;
}
}
},
type: 'AutoNumbering'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2018 10:46 PM
Hi,
You will include gs.log() in your script include.
SCRIPT INCLUDE :
var AutoNumbering = Class.create();
AutoNumbering.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getNum : function() {
var count = '1';
var gr = new GlideRecord('CUSTOM TABLE');
gr.orderByDesc('correlation_id');
gr.setLimit(1000);
gr.query();
if(gr.next())
{
//gs.log(gr.correlation_id);
if(gr.correlation_id != "")
{
var num = gr.correlation_id;
var res = num.substring(3);
var newnum = parseInt(count) + parseInt(res);
//gs.log(newnum);
//gs.log(newnum.toString());
return newnum.toString();
}
else
{
var incnewnum = '0';
//gs.log(incnewnum);
return incnewnum;
}
}
},
type: 'AutoNumbering'
});
I included log statements(as comments) remove comments and check once. So that we can find bugs.
Thanks