- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2017 01:01 AM
Hi,
We have a requirement where we need to store a location code in the cmn_location table for USA location. This attribute should be blank for all locations except USA. Only when the country is chosen as USA, the field should get auto-populated. The code should be unique and auto-incremented everytime for a new location.
The code should always be 3-digit and goes as below:
4Y0
4Y1
4Y2
.
.
.
4Y9
4Z0
4Z1
4Z2
.
.
.
4Z9
5A0
5A1
and so on.
Can anyone please help me with the script for this requirement?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2017 02:22 AM
Hi Ashish,
I did a similar kind of research a few days back, but I tried to build code on my own, below is a raw modified code as per your requirement. You should write this in a onInsert Business Rule:
var gr = new GlideRecord('cmn_location');
gr.addQuery('u_unique_id1!=NULL'); //unique_id1 is the field I used
gr.orderByDesc('u_unique_id1');
gr.query();
if(gr.next())
{
var str = gr.u_unique_id1;
var res = str.substring(2, 3); //check for last value
var resnum = parseInt(res,10);
if(resnum == 9 && str.charCodeAt(1) == "Z") //if last two values are Z9, set A0 and increment the first digit
{
var fir = str.substring(0, 1); //increment the first one
var firnxt = parseInt(fir,10);
firnxt = firnxt+1;
var nextalpupd = firnxt+'A0';
current.u_unique_id1 = nextalpupd;
}
else if(resnum == 9 && str.charCodeAt(1) != "Z") //if last two values are G9, set H0
{
var firnum = str.substring(0, 1);
var nextnumalp = str.charCodeAt(1);
var nextnumalpupd = String.fromCharCode(++nextnumalp);
var totalval = firnum + nextnumalpupd +'0';
current.u_unique_id1 = totalval;
}
else if(resnum <9 && str.charCodeAt(1) != "Z") //if last two values are B8, set B9
{
var firsttwostr = str.substring(0, 2);
var lastres = str.substring(2, 3);
var lastscen = parseInt(lastres,10);
var lastscnum = lastscen+1;
var totalvals =firsttwostr +lastscnum;
current.u_unique_id1 = totalvals;
}
else
{
current.u_unique_id1 = '0A0';
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2017 02:12 AM
You should google for an idea on how to build this in Javascript (this is not really related to platform functionality, but rather Javascript in general).
Here is a start:
jquery - auto-increment alphanumeric php , javascript - Stack Overflow

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2017 02:22 AM
Hi Ashish,
I did a similar kind of research a few days back, but I tried to build code on my own, below is a raw modified code as per your requirement. You should write this in a onInsert Business Rule:
var gr = new GlideRecord('cmn_location');
gr.addQuery('u_unique_id1!=NULL'); //unique_id1 is the field I used
gr.orderByDesc('u_unique_id1');
gr.query();
if(gr.next())
{
var str = gr.u_unique_id1;
var res = str.substring(2, 3); //check for last value
var resnum = parseInt(res,10);
if(resnum == 9 && str.charCodeAt(1) == "Z") //if last two values are Z9, set A0 and increment the first digit
{
var fir = str.substring(0, 1); //increment the first one
var firnxt = parseInt(fir,10);
firnxt = firnxt+1;
var nextalpupd = firnxt+'A0';
current.u_unique_id1 = nextalpupd;
}
else if(resnum == 9 && str.charCodeAt(1) != "Z") //if last two values are G9, set H0
{
var firnum = str.substring(0, 1);
var nextnumalp = str.charCodeAt(1);
var nextnumalpupd = String.fromCharCode(++nextnumalp);
var totalval = firnum + nextnumalpupd +'0';
current.u_unique_id1 = totalval;
}
else if(resnum <9 && str.charCodeAt(1) != "Z") //if last two values are B8, set B9
{
var firsttwostr = str.substring(0, 2);
var lastres = str.substring(2, 3);
var lastscen = parseInt(lastres,10);
var lastscnum = lastscen+1;
var totalvals =firsttwostr +lastscnum;
current.u_unique_id1 = totalvals;
}
else
{
current.u_unique_id1 = '0A0';
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2017 05:22 AM
Thanks a lot Gowrisankar.. Just a few modifications as per my requirement and worked great.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2017 03:31 AM
Hey Ashish,
.) Create a background form and create three integer fields to put the auto-increment logic (use ascii value of A-Z , easier to do increments and checks).
.) And On submit of the location on cmn_location, insert a record in the background form with sys_id of the cmn_location record and on submission get a new value using a script include which computes three integer fields and return the computed new value.
.) on cmn_location this new location field must be a reference field based on the sys_id.
Sample SI