- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2017 12:02 AM
Hello all,
I have made a table App Number, and its form has a field App No. My requirement is that when any record is created with its App No. field entry as empty, a unique number is generated for all empty entries entries in sequential order.
As per the above screenshot , in all records which are empty I want some number to be auto generated. Like for First empty entry Number is EMP1 for second its EMP2 and so on.. and the number should not be duplicated, they must be unique.
Can anybody help me with this requirement?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-08-2017 11:02 PM
Hi Arpita,
Thank you for let me know about the bug in the code.
This happened because of the order by of string field.
Here is the revised version of the BR-
(function executeRule(current, previous /*null when async*/) {
//selecting max number
var curr_max_num=0;
var next_num=0;
var gr = new GlideRecord('u_app_number');
gr.addQuery('u_app_no','CONTAINS','EMP-');
gr.query();
while(gr.next())
{
var max_str=gr.u_app_no;
var array = max_str.split('-');
var recnum=parseInt(array[1]);
if(recnum>curr_max_num)
curr_max_num= recnum;
}
var nxt_val=curr_max_num+1;
var nxt_val_str='EMP-'+nxt_val;
current.u_app_no=nxt_val_str;
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2017 12:14 AM
I have tried the following on load client script but its not working
var prefix="Emp";
var counter=0;
var t = new GlideRecord('u_app_number');
t.addQuery('u_app_no',"");
//t.addNullQuery('u_app_no');
alert("Null records");
t.query();
while (t.next())
{
alert("Loop entered");
counter += 1;
var num = counter.toString();
alert(prefix+num);
t.u_app_no=prefix+num;
t.update();
}
I have thought of an alternative to this by adding autonumber field and generate numbers only when App No. field is empty, but I'm not able to restrict auto numbering. Can I make some similar script as for auto numbering and specify a conditional statement that checks for field to be empty?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2017 12:24 AM
Hi Arpita,
How are you generating records? is it a manual process or through import process?
Thanks & Regards,
Astha Chaubey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2017 12:34 AM
Hi Astha,
The records are Imported.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2017 12:22 AM
In that case you can create a BR before insert and in condition if number is empty . Then insert the new number.
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2017 12:34 AM
Ya I'm trying the same but not able do so. can you help with the script?