Script for Unique number generation with 6 digit limit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2020 01:45 PM
Hi Forum,
Thanks in advance for helping me.
I have some requirement, we have some custom table with one custom number field, so would like to generate a unique number for this field based on the other reference field value, on the reference field we have some value, so whenever record insert into the main table we would like to get the reference field value (500) + newly generated unique value. How can I achieve this using BR.
EX: 500123456.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2020 11:51 AM
Hi Devi,
I am also setting unique number for one my string filed, so can you modify below BR as per your requirement,
After - Insert - BR
(function executeRule(current, previous /*null when async*/ ) {
var contractReq = current.u_nmber.toString();
var versionNumber = '';
var changeOrderRecord = new GlideRecord('your_table_name');
changeOrderRecord.addQuery('u_parent', contractReq);
changeOrderRecord.orderByDesc('sys_created_on'); // fetching latest record
changeOrderRecord.query();
if (changeOrderRecord.next()) {
versionNumber = changeOrderRecord.u_version_number;
versionNumber = versionNumber.replace(/[^\n\d]+/g, ''); // REGEX to return only digits from String.
versionNumber = parseInt(versionNumber);
current.u_version_number = 'V' + (versionNumber + 1);
} else {
current.u_version_number = 'V1';
}
})(current, previous);
Mark ✅ Correct if my response solves your issue and also mark 👍 Helpful if you find my answer helps you based on the impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2020 03:54 PM
Thanks for sharing it, but my requirements are different like I have to generate 6 digit sequence numbers like this 100001 for a custom field based on some reference field value. so total number will be like this 803100001, firs three are reserved digits.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2020 11:58 AM
Thank you for the replay, after I have gone through your replays, I have tried with many ways.
But my requirement is, first 4 are prefix are reserved like 1001 and then newly generated unique number with limit 6 and those unique number has to be next number to the old number (increment).
EX: if reference field has 100 and then I have to add 1 and the 6 digit number will like this
1001500000
next record will be 1001500002
if this 6 digit complete with the all numbers 599999 and the I have to add one more digit for the next unique number, so that unique number will have now 7 digit number after prefix.
EX: 10015999991
This is the script i am using now in BR
On insert, update and on before
Filter condition: correlation ID is empty.
this is for an example
(function executeRule(current, previous /*null when async*/) {
var zip1, nm2, unique, total;
current.correlation_id = "";
// Add your code here
if (current.operation() == 'insert'){
var gr = new GlideRecord("cmn_location");
gr.addQuery("name","=", location);
gr.query();
if(gr.next()){
zip1 = gr.zip; // zip code 3 digit in my example
}
total = zip1+"1";
//current.correlation_id = "123456";
//gs.addErrorMessage("this is from insert operation");
var nm = new NumberManager();
answer = nm.getNextObjNumber();
//var nm2 = nm+"3";
gs.addInfoMessage("total :"+ total);
gs.addInfoMessage("number is: "+answer);
unique = total+answer;
gs.addInfoMessage("unique number: "+unique);
current.correlation_id = unique;
}
else if (current.operation() == 'update')
{
gs.addErrorMessage("unique value should not be change");
}
})(current, previous);
how to control 6 digit number and increment in the script and make 7 digit after 6 digit.
Please help me....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2020 09:25 AM
Hi all,
Finally I got the logic for my requirements.
But the one more thing is, when we trying to load large data through transform map, we are missing the uniqueness in the number field because of transform map queue, so for that I have kept gs.sleep(1000) in BR, now its working as expected, but its taking longtime to finish to inserting records.
we tested with 250 records for them BR taking 3 to 5 min.
so how can i speedup my BR.
BR = Before, Insert
gs.sleep(1000);
try{
var zip = current.u_location2.zip;
var uniqueNumber = '';
var gr = new GlideRecord(current.getTableName());
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
if(gr.next()){
var grZip= gr.u_location2.zip.toString();
var queUniqNumb = gr.correlation_id.toString();
var corrSubStr = queUniqNumb.substring(grZip.length+1,queUniqNumb.length);
// if(corrSubStr>=100000){
uniqueNumber = zip+'3'+(parseInt(corrSubStr)+1);
// }
// else{
// uniqueNumber = zip+'3'+100000;
// }
}
current.correlation_id = uniqueNumber;
}catch(er){
gs.addErrorMessage(er);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 03:54 AM
Hi,
I have the similar requirement can you please explain the code what you have written.
what is the use of below script
var queUniqNumb = gr.correlation_id.toString();
var corrSubStr = queUniqNumb.substring(grZip.length+1,queUniqNumb.length);